ASP.NET讀寫檔案教程

來源:互聯網
上載者:User

讀取和寫入檔案使用ASP.NET ( VB )中


閱讀和寫作的檔案內容使用ASP.NET 2.0和C # 2.0其實是非常簡單的。

首先,您將需要Imports System.IO命名空間。

Imports System.IO;

我們感動我們的網站伺服器的智慧和發現他們是難以置信的專業。其安裝非常容易,我們啟動和運行中沒有時間。

System.IO命名空間包含類型,使閱讀和書寫的檔案和資料流,並類型,提供基本的檔案和目錄的支援。使用類檔案, DirectoryInfo在這裡。

使用DirectoryInfo類典型的操作,如複製,移動,重新命名,建立和刪除目錄。

使用檔案級的典型操作,如建立,開啟,刪除,添加,複製,移動和重新命名檔案。

使用ReadAllText方法開啟一個文字檔,內容所有的檔案,然後關閉該檔案。

使用WriteAllText方法建立一個新檔案,寫入指定的字串的檔案,然後關閉該檔案。如果目標檔案已經存在,它是覆蓋。

我們使用buttonDisplay_Click活動,以顯示該檔案的內容和使用buttonSave_Click甚至儲存為一個檔案。

代碼如下。www.111cn.net

Protected Sub buttonDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs)

Dim FilePath As String
FilePath = txtBoxInput.Text
If File.Exists(FilePath) Then
textBoxContents.Text = File.ReadAllText(FilePath)
Else
Response.Write("<script language='javascript'>window.alert(www.111cn.net);</script>")
Return
End If

End Sub

Protected Sub buttonSave_Click(ByVal sender As Object, ByVal e As System.EventArgs)

Dim Folder As New DirectoryInfo(textboxPath.Text)
If Folder.Exists Then
If textboxName.Text <> String.Empty Then
Dim FilePathSave As String = Folder.ToString() + textboxName.Text File.WriteAllText(FilePathSave, textBoxContents.Text)
Else
Response.Write("<script language='javascript'>window.alert('Please enter file name');</script>")
End If
Else
Response.Write("<script language='javascript'>window.alert('Folder not found');</script>")
End If

End Sub

是的,這是能夠找到一個很好的網頁主機。有時需要一段時間。經過幾次努力,我們去與伺服器智力,並已非常高興。他們是最專業的,友好的客戶服務和技術知識的東道國,我們發現迄今。

前ReadWriteTextCSharp.aspx頁看上去像這樣:

<table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#cccccc">

<tr>
<td bgcolor="#eeeeee" class="header1">
<fieldset>
<legend>ReadWriteTextCsharp</legend>
<div>
<asp:Label ID="Label1" runat="server" Text="Enter path of file to be examined and click Display"></asp:Label><br />
<asp:TextBox ID="txtBoxInput" runat="server" Width="451px"></asp:TextBox>
<asp:Button ID="buttonDisplay" runat="server" Text="Display" OnClick="buttonDisplay_Click" /><br />
<fieldset>
<legend>Content of file</legend>
<asp:Label ID="Label2" runat="server" Text="You can edit the content and save as a file."></asp:Label><br />
<table>
<tr>
<td>
<asp:TextBox ID="textBoxContents" runat="server" tabIndex="0" height="200px" textMode="MultiLine" width="450px"></asp:TextBox>
</td>
</tr>
</table>
<fieldset>
<legend>Save as</legend>Path:
<asp:TextBox ID="textboxPath" runat="server" Width="237px"></asp:TextBox>File name:
<asp:TextBox ID="textboxName" runat="server" Width="93px"></asp:TextBox>
<asp:Button ID="buttonSave" runat="server" Text="Save As" Width="66px" OnClick="buttonSave_Click" /></fieldset>
</fieldset>
</div>
</fieldset>
</td>
</tr>

</table>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.