閲覧総計:&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};
- 1 -- [[1]] &new{2013-12-03 (火) 17:21:13};
- 1 -- [[1]] &new{2013-12-03 (火) 17:21:32};
-- 1' -- [[1]] &new{2013-12-03 (火) 17:21:52};
- 1 -- [[1]] &new{2013-12-03 (火) 17:21:54};
- 1 -- [[1]] &new{2014-01-30 (木) 20:04:42};
- 1 -- [[1]] &new{2014-01-30 (木) 20:04:45};
- 1 -- [[-1']] &new{2014-01-30 (木) 20:04:47};
-- 1' -- [[1]] &new{2014-01-30 (木) 20:04:51};
- 1 -- [[1]] &new{2014-01-30 (木) 20:04:52};
- 1 -- [[1]] &new{2014-01-30 (木) 20:43:13};
- 1 -- [[1]] &new{2014-01-30 (木) 20:43:20};
- 1 -- [[-1']] &new{2014-01-30 (木) 20:43:23};
-- 1' -- [[1]] &new{2014-01-30 (木) 20:43:25};
- 1 -- [[1]] &new{2014-01-30 (木) 20:43:28};
- 1 -- [[1]] &new{2014-02-08 (土) 04:26:45};
- 1 -- [[1]] &new{2014-02-08 (土) 04:27:00};
- 1 -- [[-1']] &new{2014-02-08 (土) 04:27:05};
-- 1' -- [[1]] &new{2014-02-08 (土) 04:27:11};
- 1 -- [[1]] &new{2014-02-08 (土) 04:27:23};
- 1 -- [[1]] &new{2014-02-08 (土) 17:31:00};
- 1 -- [[1]] &new{2014-02-08 (土) 17:31:13};
- 1 -- [[-1']] &new{2014-02-08 (土) 17:31:22};
-- 1' -- [[1]] &new{2014-02-08 (土) 17:31:27};
- 1 -- [[1]] &new{2014-02-08 (土) 17:31:30};
- 1 -- [[1]] &new{2014-02-16 (日) 16:44:04};
- 1 -- [[1]] &new{2014-02-16 (日) 16:44:08};
- 1 -- [[-1']] &new{2014-02-16 (日) 16:44:10};
-- 1' -- [[1]] &new{2014-02-16 (日) 16:44:11};
- 1 -- [[1]] &new{2014-02-16 (日) 16:44:13};

#comment_nospam
- 1 -- [[1]] &new{2014-02-16 (日) 16:44:06};
- 1 -- [[1]] &new{2014-02-08 (土) 17:31:10};
- 1 -- [[1]] &new{2014-02-08 (土) 04:26:55};
- 1 -- [[1]] &new{2014-02-07 (金) 09:11:55};
- 1 -- [[1]] &new{2014-01-30 (木) 20:43:17};
- 1 -- [[1]] &new{2014-01-30 (木) 20:04:44};
- 1 -- [[1]] &new{2013-12-03 (火) 17:21:20};
- 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])


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS