I want to format (FAT32) a removable disk with C # programming. I found a way on the Internet, but the problem is that it opens a general Windows format program. But I want to do this only with C # and without native Windows support.
My method:
// FAT32 Format Button click event [DllImport("shell32.dll")] static extern uint SHFormatDrive(IntPtr hwnd, uint drive, uint fmtID, uint options);
You can use wmi , there is a way that resolves this.
http://msdn.microsoft.com/en-us/library/aa390432%28v=VS.85%29.aspx
I do not believe that in C # there is something that in the general case will format a disk of any format. The method that you have is probably the best way to do this on Windows.
, , - , . # -.
, -, , . , , (, ), .
SHFormatDrive - API DeviceIoCtl, , . , , .
SHFormatDrive
DeviceIoCtl
, p/invoke'ng DeviceIoCtl, . ( SysInternals) "FormatX", NT4 , DeviceIOCtl , , dicontinued, . , - , format . format /?, . , System.Diagnostics.Process ( cmd.exe /c).
format
format /?
cmd.exe /c
WMI:
var query = String.Format("SELECT * FROM Win32_Volume WHERE Name = '{0}'", "E:\\\\"); ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", query); var queryCollection = from ManagementObject x in searcher.Get() select x; var volume = queryCollection.FirstOrDefault(); var resultCode = volume.InvokeMethod("Format", new object[] { "FAT32", true, 4096, "Volume Name", false });