6月 12

一度にすべて読み込む

テキストファイルの中身を
一度にすべて読み込んで表示するサンプルです。

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();

6月 12

ファイルの移動/ファイル名の変更

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" );

6月 12

ファイルをごみ箱に捨てる

まず参照設定に 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);  // ごみ箱に送る
        }
    }
}

6月 12

ファイルをコピー

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 );

6月 12

論理ドライブ一覧を取得する

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:\
6月 12

デスクトップディレクトリの物理的なフルパスを取得する

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);
        }
    }
}

6月 12

ディレクトリのサブディレクトリ一覧を取得する

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