[ C# ] ドライブをフォーマットする ( Win32_Volume.Format )

Pocket

ここでは、WMI の Win32_Volume.Format 関数を使用して論理ドライブをフォーマットするサンプルコードを掲載しています。

スポンサーリンク

ドライブのフォーマット

WMI の Win32_Volume.Format 関数を使用して、論理ドライブをフォーマットするサンプルコードは次の通りです。詳細はコメントも参照ください。

// 参照の追加を行う必要あり
//using System.Management;

string driveLetter = @"K:";         // ドライブレター
string fileSystem = @"FAT32";      // NTFS, FAT, FAT32 
bool quickFormat = true;          // クイックフォーマット
UInt32 clusterSize = 8192;          // クラスタサイズ
string label = @"VOL_LABEL";  // ボリュームラベル
bool enableCompression = false;         // 圧縮(Format関数では未実装らしい)

// ボリューム情報を取得
ManagementObjectSearcher sea;
string query = @"select * from Win32_Volume WHERE DriveLetter = '" + driveLetter + "'";
sea = new ManagementObjectSearcher(query);

foreach (ManagementObject mo in sea.Get())
{
    // Format 関数のパラメータ設定
    ManagementBaseObject inParams = mo.GetMethodParameters("Format");

    inParams["FileSystem"] = fileSystem;
    inParams["QuickFormat"] = quickFormat;
    inParams["ClusterSize"] = clusterSize;
    inParams["Label"] = label;
    inParams["EnableCompression"] = enableCompression;

    // フォーマット実行
    ManagementBaseObject outParams;
    outParams = mo.InvokeMethod("Format", inParams, null);
    if (outParams["returnValue"].ToString() != "0")
    {
        // エラー発生
    }
}

Format 関数のリターン値

Win32_Volume.Format 関数のリターン値は下表のとおりです。下段の参考リンクより詳細を確認ください。

リターン値 説明
0 Success
1 Unsupported file system
2 Incompatible media in drive
3 Access denied
4 Call canceled
5 Call cancellation request too late
6 Volume write protected
7 Volume lock failed
8 Unable to quick format
9 Input/Output (I/O) error
10 Invalid volume label
11 No media in drive
12 Volume is too small
13 Volume is too large
14 Volume is not mounted
15 Cluster size is too small
16 Cluster size is too large
17 Cluster size is beyond 32 bits
18 Unknown error

 

参考

 

スポンサーリンク


Pocket

Leave a Comment

Your email address will not be published. Required fields are marked *