ASP. NET File Processing
One type Program The size of the file processing capability is also a manifestation of the function size of a language. In ASP. net.. NET Framework SDK. io namespace. the IO namespace defines many class, method, and attributes about file processing. These classes, methods, and attributes make it easy to use ASP.. net. Many classes, methods, and attributes are defined in this namespace.
Basic file operations are commonly used. Specifically, it refers to reading, writing, copying, moving, and deleting objects. These operations are described in detail below.
1) Main Ideas and steps in program design:
The program language used in this ASP. NET page is VB. NET. The main function of the program is to open a specified file and display the file in ASCII code. The following are some main steps in programming:
I>. Read the objects used in the file-streamreader
In system. Io, A streamreader object is defined to read files. This program reads files through this object. It is displayed in text format. The details are as follows:
Dim streamreaderobj as streamreader
File_name = filename. postedfile. filename
'Read a specified file and assign it to the streamreader object.
Do
Filecont = streamreaderobj. Readline ()
Response. Write (filecont & "<br> ")
Loop until filecont = ""
'Only when one row reads the file content and one row of content is null string
Streamreaderobj. Close
'After reading, close the streamreader object.
II> the form used in the program must be encoded as "multipart/form-Data" without its default encoding. The details are as follows:
<Form method = "Post" enctype = "multipart/form-Data" runat = "server">
</Form>
2). ProgramSource code:
<% @ import namespace = "system. io "%>
'After reading, close the streamreader object.
Streamreaderobj. Close
Response. Write ("<br> the above is all the content of the" & file_name & "file! ")
End sub
</SCRIPT>
<Asp: Panel id = "Panel1" runat = "server">
<Form method = "Post" enctype = "multipart/form-Data" runat = "server">
<P> welcome to read files on the ASP. NET page </P>
Select a file name: <input type = "file" id = "FILENAME" runat = "server"/> <br>
<Asp: button id = "write" text = "Read File" onclick = "sendbutton_click" runat = "server"/>
</Form>
</ASP: Panel>
3. Write files on ASP. NET pages:
1) Main Ideas and steps in program design:
The program language used in this ASP. NET page is VB. NET. The main function of the program is to create a text file, which can be defined by the user, and then save the content entered by the user in this file. The main steps of program design are as follows:
I>. Read the objects used in the file-streamwriter
Streamwriter is also defined by the namespace -- system. Io and is mainly used to write file content. This program also uses it to complete file write operations. It is easier to write files than to read files. The procedure is as follows:
Dim strwriterobj as streamwriter
Dim contxt as string
File_name = filename. Text
Contxt = body. Text
'Body. Text is the content of the file to be written.
Strwriterobj = file. createtext (file_name)
'Create a file and assign the text file to the streamwriter object as an object.
Strwriterobj. writeline (contxt)
'Write text content into the file
Strwriterobj. Close
'Close the object
II>. What we have done above is to create a new file and write the content. If the file to be written already exists, the program clears the original file and then writes new content. If you want to complete the work, you only need to add content to the end of an existing file, and create and Append content to a non-existing file. Replace the following statement.
Replaced statement:
Strwriterobj = file. createtext (file_name)
Replace the idiom:
Strwriterobj = file. appendtext (file_name)
2). Program SourceCode
<% @ Import namespace = "system. Io" %>
<Script language = "VB" runat = "server">
Sub sendbutton_click (sender as object, e as eventargs)
'Define A streamwriter object
Dim strwriterobj as streamwriter
Dim file_name as string
Dim contxt as string
'Create a file and assign the text file to the streamwriter object as an object.
File_name = filename. Text
Contxt = body. Text
Strwriterobj = file. createtext (file_name)
'Write text content into the file
Strwriterobj. writeline (contxt)
'Close the object
Strwriterobj. Close
Response. Write ("the file write operation is successful! ")
Panel1.visible = false
End sub
</SCRIPT>
<Asp: Panel id = "Panel1" runat = "server">
<Form ID = "form1" runat = "server">
<P> welcome to write files on the ASP. NET page </P>
Enter the file name: <asp: textbox id = "FILENAME" size = "40" runat = "server"/> <br>
Enter the written content: <asp: textbox id = "body" textmode = "multiline" columns = "40" rows = "10" runat = "server"/> <br>
<Asp: button runat = "server" id = "write" text = "Write File" onclick = "sendbutton_click"/>
</Form>
</ASP: Panel>
4. Copy, move, and delete files on the ASP. NET page
It is easier to copy, move, and delete files in ASP. NET. This is because the method for copying and moving objects is defined in the namespace system. Io. You can directly call these methods to complete the preceding operations. The following example shows how to copy, move, and delete a file.
1). file copy operation
<% @ Import namespace = "system. Io" %>
<%
'The following statement completes file copy
File. Copy ("C: \ sample.txt", "C: \ temp.txt ")
Response. Write ("the file copy has been completed successfully! ")
%>
2). File Movement
<% @ Import namespace = "system. Io" %>
<%
'The following statement completes file Movement
File. Move ("C: \ sample.txt", "d: \ sample.txt ")
Response. Write ("file movement has been completed successfully! ")
%>
3). File Deletion
<% @ Import namespace = "system. Io" %>
<%
'The following statement deletes the file
File. Delete ("C: \ sample.txt ")
Response. Write ("file deletion has been completed successfully! ")
%>
V. Summary:
If you want to use ASP. NET flexibly and fully understand the powerful functions of. NET and implement the. NET Framework SDK developed by Microsoft for the. NET Framework. You can only master the content in the. NET Framework SDK.