System.IO.FileInfo の Lengthプロパティーを参照し、
指定ファイルのサイズをバイト単位で取得します。
System.IO.FileInfo fi = new System.IO.FileInfo( @"c:\hello.txt" ); MessageBox.Show( fi.Length.ToString() );
System.IO.FileInfo の Lengthプロパティーを参照し、
指定ファイルのサイズをバイト単位で取得します。
System.IO.FileInfo fi = new System.IO.FileInfo( @"c:\hello.txt" ); MessageBox.Show( fi.Length.ToString() );
テキストファイルの中身を
一度にすべて読み込んで表示するサンプルです。
System.IO.StreamReaderクラスを使います。
同クラスのコンストラクタにて
テキストファイル名と
エンコード方式を与えます。
あまり大きなサイズのファイルだと
読み込みに時間がかかってしまうので
サンプルを動かす場合は
小さめのファイルでまずは試してください。
最後にStreamをクローズすることを忘れずに。
【補足】
ReadToEnd()の戻り値はstringなので
加工したい場合は
1度string型の変数か何かで受けてご自由に。
System.IO.StreamReader m_reader = null; m_reader = new System.IO.StreamReader( @"c:\hello.txt", System.Text.Encoding.Default ); MessageBox.Show( m_reader.ReadToEnd() ); m_reader.Close();
System.IO.File.Move()関数を使います。
移動元ファイル名と
移動先ファイル名を指定します。
// ファイルを移動する(移動の際、ファイル名を変更しても構いません) // // ※移動元ファイルが無い場合 // 例外:System.IO.FileNotFoundException が発生します。 // ※移動先ファイルが既に存在する場合 // 例外:System.IO.IOException が発生します。 System.IO.File.Move( @"C:\hello.txt", @"C:\Temp\hello.txt" ); // 移動元と移動先のフォルダが同じであれば名前を変更したことになります。 System.IO.File.Move( @"C:\hello.txt", @"C:\unko.txt" );
まず参照設定に Microsoft.VisualBasic.dll を追加します。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Text; using System.Windows.Forms; using Microsoft.VisualBasic.FileIO; // これを追加 namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { FileSystem.DeleteFile( @"c:\temp\hogehoge.dat", // ごみ箱に捨てるファイル名 UIOption.OnlyErrorDialogs, // ファイル移動中のダイアログはエラーのみ RecycleOption.SendToRecycleBin); // ごみ箱に送る } } }
System.IO.File.Delete()関数を使います。
ファイル名を指定し、削除します。
System.IO.File.Delete( @"c:\unko.txt" );
System.IO.File.Copy()関数を使います。
コピー元ファイル名と
コピー先ファイル名を指定します。
// unko.txt を hello.txt としてコピー // // ※コピー元ファイルが存在しない場合 // 例外:System.IO.FileNotFoundException が発生します。 // ※コピー先ファイルが既に存在する場合 // 例外:System.IO.IOException が発生します。 System.IO.File.Copy( @"C:\unko.txt", @"C:\hello.txt" ); // コピー先ファイルが既に存在する場合、上書き(第3パラメータ:true) // // ※コピー元ファイルが存在しない場合 // 例外:System.IO.FileNotFoundException が発生します。 System.IO.File.Copy( @"C:\unko.txt", @"C:\hello.txt", true );
System.IO.File.Exists()関数を使います。
指定ファイルが存在するか否かを調べ
結果の真偽値を得ます。
System.IO.File.Exists()関数を利用します。
if ( System.IO.File.Exists(@"c:\HP\index.html") ) MessageBox.Show( "あったよママン!" ); else MessageBox.Show( "ないよぉ orz" );
MD5CryptoServiceProviderクラスを利用します。
ComputeHash関数でbyte配列を取得し
BitConverterでbyte配列を16進数文字列に変換しています。
private string GetMD5FromFile(string filename) { string ret = string.Empty; if (File.Exists(filename)) { System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read); byte[] bytehash = md5.ComputeHash(fs); ret = BitConverter.ToString(bytehash).Replace("-", string.Empty); } return ret; }