Answers for Day 13

IOTA^_^    

Sams Teach Yourself ASP.NET in 21 Days, Second Edition
By Chris Payne
Table of Contents
Appendix A.  Answers to Quiz Questions


Quiz

1:

What is the difference between a file and a stream?

A1:

A file is a physical collection of data residing on a computer. A stream is a way to access data from different locations, such as a file, memory, or over the network.

2:

What are the six FileMode values?

A1:

Append, Open, OpenOrCreate, Create, CreateNew, and Truncate

3:

What does the Peek method do, and why is it helpful?

A1:

The Peek method reads the next character from a stream, but doesn't actually return the character. It is useful for determining when you're at the end of a stream.

4:

How do you verify the existence of a file or directory on a server?

A1:

The FileExists or DirectoryExists methods from the File and Directory classes, respectively.

5:

Use the File object to open a file called userdata.txt in the root directory of your c: drive, for appending. If this file doesn't exist, use the File object to create it, and make sure that no other applications can access this file while you are using it.

A1:

 dim f as new File("c:\userdata.txt") f.Open(FileMode.Append, FileAccess.ReadWrite, FileShare.None) 

Exercises

1:

Create an ASP.NET page that will open a file specified by a user in a text box, and display the contents in a label. Make sure to check whether the file exists. Note that the user must be required to enter the full path to the file as well.

A1:

The code for the answer is as follows:

 1:  <%@ Page Language="VB" %> 2:  <%@ Import namespace="System.IO" %> 3: 4:  <script runat="server"> 5:     sub DisplayFile(Sender as Object, e as EventArgs) 6:        dim strFile as string = tbFile.Text 7:        dim objReader as StreamReader 8: 9:        if strFile <> "" then 10:           if File.Exists(strFile) then 11:              objReader = new StreamReader(strFile) 12: 13:              lblMessage.Text = "<pre>" 14:              while objReader.Peek > -1 15:                 lblMessage.Text += Server.HTMLEncode _ 16:                    (objReader.ReadLine) & "<br>" 17:              end while 18:              lblMessage.Text += "</pre>" 19: 20:              objReader.Close 21:           end if 22:        end if 23:     end sub 24:  </script> 25: 26:  <html><body> 27:     <form runat="server"> 28:        <asp:TextBox  runat="server" 29:           OnTextChanged="DisplayFile" 30:           AutoPostBack=true /><p> 31: 32:        <asp:Label  runat="server" 33:           MaintainState=false/> 34:     </form> 35:  </body></html> 
2:

Modify Exercise 1 so that the content of the file is displayed in an editable text box. Allow the user to make changes, and when the user clicks a submit button, write the changes back to the file.

A1:

 1:  <%@ Page Language="VB" %> 2:  <%@ Import namespace="System.IO" %> 3: 4:  <script runat="server"> 5:     sub Page_Load(Sender as Object, e as EventArgs) 6:        if not Page.IsPostBack then 7:           tbContents.Visible = false 8:        end if 9:     end sub 10: 11:     sub DisplayFile(Sender as Object, e as EventArgs) 12:        dim strFile as string = tbFile.Text 13:        dim objReader as StreamReader 14: 15:        if strFile <> "" then 16:           if File.Exists(strFile) then 17:              if not tbContents.Visible then 18:                 tbContents.Visible = true 19:              else 20:                 tbContents.Text = "" 21:              end if 22: 23:              objReader = new StreamReader(strFile) 24: 25:              tbContents.Text = Server.HTMLEncode _ 26:                    (objReader.ReadToEnd) 27:              objReader.Close 28:           else 29:              tbContents.Text = "Invalid file" 30:           end if 31:        end if 32:     end sub 33: 34:     sub WriteFile(Sender as Object, e as EventArgs) 35:        dim strContents as string = tbContents.Text 36:        dim strFile as string = tbFile.Text 37:        dim objWriter as StreamWriter 38: 39:        if File.Exists(strFIle) then 40:           objWriter = new StreamWriter(strFile, false) 41: 42:           objWriter.Write(strContents) 43: 44:           objWriter.Close 45:        end if 46: 47:     end sub 48:  </script> 49: 50:  <html><body> 51:     <form runat="server"> 52:        <asp:TextBox  runat="server" 53:           OnTextChanged="DisplayFile" 54:           AutoPostBack=true /><p> 55: 56:        <asp:TextBox  runat="server" 57:           visible=true 58:           TextMode="Multiline" 59:           Columns="50" 60:           rows="20" /><p> 61: 62:        <asp:Button  runat="server" 63:           OnClick="WriteFile" 64:           Text="Submit"/> 65:     </form> 66:  </body></html> 


    IOTA^_^    
    Top


    Sams Teach Yourself ASP. NET in 21 Days
    Sams Teach Yourself ASP.NET in 21 Days (2nd Edition)
    ISBN: 0672324458
    EAN: 2147483647
    Year: 2003
    Pages: 307
    Authors: Chris Payne

    Similar book on Amazon

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net