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" );
System.EnvironmentクラスのGetLogicalDrives関数を利用します。
戻り値はドライブ名文字列の配列です。
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string[] drives = System.Environment.GetLogicalDrives(); foreach (string drive in drives) Console.WriteLine(drive); } } }
■結果
C:\ E:\
System.EnvironmentクラスのGetFolderPath関数を利用します。
引数にはデスクトップを表すEnvironment.SpecialFolder.DesktopDirectoryを指定します。
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string desktop = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); Console.WriteLine(desktop); } } }
System.IO.DirectoryInfoクラスのGetDirectories関数を利用します。
戻り値は同じくサブディレクトリのSystem.IO.DirectoryInfoクラスの配列です。
namespace ConsoleApplication1 { class Program { static void Main(string[] args) { // Cドライブ直下のサブディレクトリ一覧を取得 System.IO.DirectoryInfo dirinfo = new System.IO.DirectoryInfo(@"c:\"); System.IO.DirectoryInfo[] subdir = dirinfo.GetDirectories(); foreach (System.IO.DirectoryInfo info in subdir) Console.WriteLine(info.FullName); } } }
■結果
c:\Documents and Settings c:\HP c:\HP old c:\MSOCache c:\Program Files c:\RECYCLER c:\src c:\System Volume Information c:\temp c:\Tosutils c:\WINDOWS c:\win_tool c:\WTL80