how to write directly to the hard disk drive ?

  • Thread starter Thread starter =?ISO-8859-1?Q?Herbert_VON_GR=DCNENWALD?=
  • Start date Start date
?

=?ISO-8859-1?Q?Herbert_VON_GR=DCNENWALD?=

Hi everybody !

I would like to know if there is a C# class that do that.

(i know i can use Kernel32.dll and the C++ API CreateFile)

thanks
 
Are you just looking to write a file? The system.IO class has most of the
methods you will need.

FileStream file = new FileStream("path.txt", FileMode, FileAccess);
StreamWriter sw = new StreamWriter(file);
sw.Write("Hello file system world!");
sw.Close();

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director
 
Herbert VON GRÜNENWALD said:
Hi everybody !

I would like to know if there is a C# class that do that.

(i know i can use Kernel32.dll and the C++ API CreateFile)

thanks

Using PInvoke you can call CreateFile and you can wrap the handle in a
Fiilestream like this:

IntPtr handle = CreateFile(FileName,......);

FileStream stream = new FileStream(handle,....);


Willy.
 
John Timney (ASP.NET MVP) said:
Are you just looking to write a file? The system.IO class has most of the
methods you will need.

FileStream file = new FileStream("path.txt", FileMode, FileAccess);
StreamWriter sw = new StreamWriter(file);
sw.Write("Hello file system world!");
sw.Close();

John,

Guess you missed the subject.

Willy.
 
John said:
Are you just looking to write a file? The system.IO class has most of the
methods you will need.

FileStream file = new FileStream("path.txt", FileMode, FileAccess);
StreamWriter sw = new StreamWriter(file);
sw.Write("Hello file system world!");
sw.Close();


thanks for your response, but i would like to write the MBR

in C++ i've used CreateFile with \\.\PHYSICALDRIVE0
 
so it would seem....lol

--
Regards

John Timney
ASP.NET MVP
Microsoft Regional Director
 
Willy said:
Using PInvoke you can call CreateFile and you can wrap the handle in a
Fiilestream like this:

IntPtr handle = CreateFile(FileName,......);

FileStream stream = new FileStream(handle,....);


Willy.

i've found a method to enumerates the hard disk drives:

***************************************************************
SelectQuery sq = new System.Management.SelectQuery("Win32_DiskDrive");
ManagementObjectSearcher searcher = new
System.Management.ManagementObjectSearcher(sq);

foreach (System.Management.ManagementObject mo in searcher.Get())
{
Log.Fichier.write(mo.GetText(System.Management.TextFormat.Mof), 0);
str = mo.GetPropertyValue("Model").ToString();
Log.Fichier.write(str, 0);
}
***************************************************************

but is there another class to manipulate them ?

thanks
 
Herbert VON GRÜNENWALD said:
i've found a method to enumerates the hard disk drives:

***************************************************************
SelectQuery sq = new System.Management.SelectQuery("Win32_DiskDrive");
ManagementObjectSearcher searcher = new
System.Management.ManagementObjectSearcher(sq);

foreach (System.Management.ManagementObject mo in searcher.Get())
{
Log.Fichier.write(mo.GetText(System.Management.TextFormat.Mof), 0);
str = mo.GetPropertyValue("Model").ToString();
Log.Fichier.write(str, 0);
}
***************************************************************

but is there another class to manipulate them ?

thanks

Not sure what you mean with manipulate them, but if your intention is to
read/write devices you have to open the device using Win32's CreateFile just
like you did in C, wrap the handle in a FileStream and do whatever you like
with the device.

Consider following snippet...


// DllImport signature for CreateFile.
[DllImport("kernel32", SetLastError=true)]
static extern IntPtr CreateFile(

string FileName, // file name

int DesiredAccess, // access mode

System.IO.FileShare ShareMode, // share mode

IntPtr SecurityAttributes, // Security Attr

System.IO.FileMode CreationDisposition, // creation mode

int FlagsAndAttributes, // file attributes

IntPtr hTemplate // template file

);


const int FILE_FLAG_NO_BUFFERING = 0x20000000);
// Open the device for reading, disable buffering
IntPtr handle = CreateFile( "\\.\PHYSICALDRIVE0",
FileAccess.Read,

FileShare.None,

IntPtr.Zero,

FileMode.Open,

FILE_FLAG_NO_BUFFERING,

IntPtr.Zero);

// Wrap the handle in a stream

FileStream stream = new FileStream(handle, acc, blockSize, async);

// use the stream to read from the device

....



Willy.
 
Willy said:
i've found a method to enumerates the hard disk drives:

***************************************************************
SelectQuery sq = new System.Management.SelectQuery("Win32_DiskDrive");
ManagementObjectSearcher searcher = new
System.Management.ManagementObjectSearcher(sq);

foreach (System.Management.ManagementObject mo in searcher.Get())
{
Log.Fichier.write(mo.GetText(System.Management.TextFormat.Mof), 0);
str = mo.GetPropertyValue("Model").ToString();
Log.Fichier.write(str, 0);
}
***************************************************************

but is there another class to manipulate them ?

thanks


Not sure what you mean with manipulate them, but if your intention is to
read/write devices you have to open the device using Win32's CreateFile just
like you did in C, wrap the handle in a FileStream and do whatever you like
with the device.

Consider following snippet...


// DllImport signature for CreateFile.
[DllImport("kernel32", SetLastError=true)]
static extern IntPtr CreateFile(

string FileName, // file name

int DesiredAccess, // access mode

System.IO.FileShare ShareMode, // share mode

IntPtr SecurityAttributes, // Security Attr

System.IO.FileMode CreationDisposition, // creation mode

int FlagsAndAttributes, // file attributes

IntPtr hTemplate // template file

);


const int FILE_FLAG_NO_BUFFERING = 0x20000000);
// Open the device for reading, disable buffering
IntPtr handle = CreateFile( "\\.\PHYSICALDRIVE0",
FileAccess.Read,

FileShare.None,

IntPtr.Zero,

FileMode.Open,

FILE_FLAG_NO_BUFFERING,

IntPtr.Zero);

// Wrap the handle in a stream

FileStream stream = new FileStream(handle, acc, blockSize, async);

// use the stream to read from the device

...



Willy.
thanks
 
Back
Top