File processing in asp.net (3)

Source: Internet
Author: User
asp.net three. ASP. NET page for file write operations:

The running interface of this program is shown in the following illustration:


Figure 3: Program Run interface


1. The 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-form file, which can be defined by the user, and then save the user's content in this file. The following are the main steps of programming:

i>. Read the objects used in the file--streamwriter

StreamWriter is also defined in the namespace--system.io, which is used primarily to write file content. In this procedure also through him to complete the file write operation. It is simpler to compare the file's write operations to the read operation of the file. The specific steps are as follows:

Dim Strwriterobj as StreamWriter
Dim Contxt as String
file_name = Filename.text
Contxt = Body.text
' Where Body.text is the content to write to the file
Strwriterobj = File.createtext (file_name)
' Create a file and assign the text file as an object to the StreamWriter object
Strwriterobj. WriteLine (Contxt)
' Write the text content in the file
Strwriterobj. Close
' Close Object

ii>. The above main completion is to create a new file and write the content. If the file to be written is already present, the program empties the original file and then writes the new content. If all you have to do is add content to a file that already exists, just the end of the file, create and append to a nonexistent file. Replace with the following statement, it's OK.

Replaced statement:

Strwriterobj = File.createtext (file_name)

Replace idiom sentence:

Strwriterobj = File.appendtext (file_name)

2). Program Source Code

<% @ 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 as an object to the StreamWriter object
file_name = Filename.text
Contxt = Body.text
Strwriterobj = File.createtext (file_name)
' Write the text content in the file
Strwriterobj. WriteLine (Contxt)
' Close Object
Strwriterobj. Close
Response.Write ("Successful file write operation!") " )
Panel1.visible = False
End Sub
</script>
<asp:panel id= "Panel1" runat= "Server"
<form id= "Form1" runat= "Server"
<p> Welcome to write to file on asp.net page </p
Please fill in the file name: <asp:textbox id = "FileName" size = "runat" = "Server"/><BR "
Please fill in the Write content: <asp:textbox id = "Body" TextMode = "MultiLine" Columns = "Rows" = "ten" runat = "server"/><BR
<asp:button runat = "server" id = "Write" Text = "Write file" OnClick = "Sendbutton_click"/>
</form >
</asp:panel >


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.