閲覧総計:&counter(); (本日:&counter(today); 昨日:&counter(yesterday);) ASP.NETでのテキストファイルの読込みと書出しの方法を確認した。 09/08/12 .NET Framework2.0からFile.WriteAllText(), File.ReadAllText()メソッド が提供されテキストファイルのハンドリングが非常に簡単になっている。 以下に提示したコードは冗長的になっている部分があるが、基本機能を示すサン プルコードの提示であるので了解頂きたい。 09/08/12 (背景) ASP.NET開発はSQL Server等のDBMSを利用する事が前提になっており、テキスト ファイルへの書出しの方法などあまり紹介されていないので、今回確認した。 開発環境:VWD2005+AJAX1.0+Toolkit + SQL Server2005 サーバ:ASP.NET2.0+AJAX1.0 + SQL Server2005 【稼動サンプル】 http://www.kuri6005.useiis7.net/4AspNet10/TextRW/TextRW.aspx #ref(textRW01.JPG); コントロールの設定値 |~コントロール|~プロパティ|~値|~コメント| |TextBox1|TextMode|MultiLine|複数行表示に設定| 【TextRW.aspx】 <%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> '参考ページ:テキストファイルの入出力 07/04/18 'http://migelnanai.blog.so-net.ne.jp/2007-04-18-1 Protected Sub btnWrite_Click(ByVal sender As Object, ByVal e As System.EventArgs) Dim strFile As String = Server.MapPath("test01.txt") '入力するテキスト Dim strInputText As String = TextBox1.Text 'Shift_JISのファイルに書き込む。 Dim enc As System.Text.Encoding = System.Text.Encoding.GetEncoding("Shift_JIS") System.IO.File.WriteAllText(strFile, strInputText, enc) End Sub Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Dim strFile As String = Server.MapPath("test01.txt") '入力するテキスト Dim strInputText As String = TextBox1.Text 'Shift_JISのファイルに書き込む。 Dim enc As System.Text.Encoding = System.Text.Encoding.GetEncoding("Shift_JIS") System.IO.File.AppendAllText(strFile, strInputText, enc) End Sub Protected Sub btnRead_Click(ByVal sender As Object, ByVal e As System.EventArgs) 'Dim strFile As String = "ファイル名" Dim strFile As String = Server.MapPath("test01.txt") 'Shift_JISのファイルを読み込む。 Dim enc As System.Text.Encoding = System.Text.Encoding.GetEncoding("Shift_JIS") 'テキストファイルの中身をすべて読み込む Dim strText As String = System.IO.File.ReadAllText(strFile, enc) TextBox1.Text = strText End Sub Protected Sub btnDel_Click(ByVal sender As Object, ByVal e As System.EventArgs) 'Dim strFile As String = "test01.txt" Dim strFile As String = Server.MapPath("test01.txt") '入力するテキスト Dim strInputText As String = "" TextBox1.Text = "" 'Shift_JISのファイルに書き込む。 Dim enc As System.Text.Encoding = System.Text.Encoding.GetEncoding("Shift_JIS") System.IO.File.WriteAllText(strFile, strInputText, enc) End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>無題のページ</title> </head> <body> <form id="form1" runat="server"> <div> テキストファイルの書込み・読込み 09/08/12<br /> <br /> 【機能】テキストファイルの上書き書込み、追加書込<br /> み、読み出し、消去を行う。<br /> <asp:TextBox ID="TextBox1" runat="server" Height="175px" Width="327px" TextMode="MultiLine"></asp:TextBox><br /> <asp:Button ID="btnWrite" runat="server" OnClick="btnWrite_Click" Text="上書き書込み" /> <asp:Button ID="btnAdd" runat="server" Text="追加書込み" OnClick="btnAdd_Click" /> <asp:Button ID="btnRead" runat="server" Text="読出し" OnClick="btnRead_Click" /> <asp:Button ID="btnDel" runat="server" Text="消去" OnClick="btnDel_Click" /><br /> </div> </form> </body> </html> 【参考にしたページ】 1.テキストファイルの入出力 07/04/18 http://migelnanai.blog.so-net.ne.jp/2007-04-18-1 2.テキスト・ファイルの内容を簡単に書き込むには? 07/12/06 http://www.atmarkit.co.jp/fdotnet/dotnettips/680filewriteall/filewriteall.html 【サーバ設置時のハマリポイント】 txtファイルの書込み・読込みプログラムをサーバに設置した時、最初はうまく 動いてくれなかった。原因は、対象フォルダの「書き込み権限」の許可がされていな かったからだった。 皆さん、サーバ対象フォルダの「書き込み権限」の許可設定を忘れないようにしま しょう。 09/08/12 (設定操作) サーバにloginし、「ファイルのフォルダの管理(ファイルマネジャ)」で 対象プログラムを設置したフォルダのWeb Visitorの「書き込み」にチェックを付け、 「Apply」ボタンをクリックし、情報更新を行う。 #ref(textRW02.JPG); - 1 -- [[1]] &new{2012-05-22 (火) 10:33:03}; - 1 -- [[1]] &new{2012-05-22 (火) 10:33:06}; - 1 -- [[-1']] &new{2012-05-22 (火) 10:33:08}; -- 1' -- [[1]] &new{2012-05-22 (火) 10:33:09}; - 1 -- [[1]] &new{2012-05-22 (火) 10:33:10}; - 1 -- [[1]] &new{2012-07-11 (水) 09:13:59}; - 1 -- [[1]] &new{2012-07-11 (水) 09:14:02}; - 1 -- [[-1']] &new{2012-07-11 (水) 09:14:03}; -- 1' -- [[1]] &new{2012-07-11 (水) 09:14:04}; - 1 -- [[1]] &new{2012-07-11 (水) 09:14:05}; - 1 -- [[1]] &new{2013-02-08 (金) 23:01:50}; - 1 -- [[1]] &new{2013-02-08 (金) 23:01:54}; - 1 -- [[-1']] &new{2013-02-08 (金) 23:01:56}; -- 1' -- [[1]] &new{2013-02-08 (金) 23:01:58}; - 1 -- [[1]] &new{2013-02-08 (金) 23:01:59}; - 1 -- [[1]] &new{2013-10-03 (木) 11:59:51}; - 1 -- [[1]] &new{2013-11-19 (火) 11:18:05}; - 1 -- [[1]] &new{2013-11-19 (火) 11:18:14}; - 1 -- [[-1']] &new{2013-11-19 (火) 11:18:20}; -- 1' -- [[1]] &new{2013-11-19 (火) 11:18:23}; - 1 -- [[1]] &new{2013-11-19 (火) 11:18:29}; #comment_nospam - 1 -- [[1]] &new{2013-11-19 (火) 11:18:08}; - 1 -- [[1]] &new{2013-10-03 (木) 11:58:47}; - 1 -- [[1]] &new{2013-02-08 (金) 23:01:53}; - 1 -- [[1]] &new{2012-07-11 (水) 09:14:01}; - 1 -- [[1]] &new{2012-05-22 (火) 10:33:05}; #vote(参考になった[1],ふつう[0],参考にならなかった[0])