P / Invoke GetDiskFreeSpaceEx

Exact duplicate: Getting the size (free, total) of a Windows Mobile mobile drive using C #


expensive,

I know that my problem took a long time, and many of them helped me, but I am new to C # and this is my first application.

Now I read the article:

C # Signature:

  [DllImport("coredll.dll", SetLastError=true, CharSet=CharSet.Auto)]
  [return: MarshalAs(UnmanagedType.Bool)]
  static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
                                        out ulong lpFreeBytesAvailable,
                                        out ulong lpTotalNumberOfBytes,
                                        out ulong lpTotalNumberOfFreeBytes);

Code example:

   ulong FreeBytesAvailable;
   ulong TotalNumberOfBytes;
   ulong TotalNumberOfFreeBytes;

   bool success = GetDiskFreeSpaceEx("C:\\", out FreeBytesAvailable, out  
                                    TotalNumberOfBytes,out TotalNumberOfFreeBytes);
   if (!success)
         throw new System.ComponentModel.Win32Exception();

   Console.WriteLine("Free Bytes Available:      {0,15:D}", FreeBytesAvailable);
   Console.WriteLine("Total Number Of Bytes:     {0,15:D}", TotalNumberOfBytes);
   Console.WriteLine("Total Number Of FreeBytes: {0,15:D}", TotalNumberOfFreeBytes);

Now, how do I use this GetDiskFreeSpaceEx function, and should I add a C # signature somewhere?!? and what about coredll.dll?!?

my code looks like this:

  FolderInfo = (CONADefinitions.CONAPI_FOLDER_INFO)Marshal.PtrToStructure(Buffer,     
                                      typeof(CONADefinitions.CONAPI_FOLDER_INFO));
  if (FolderInfo.pstrName[0].ToString() != "C" && level == 0) 
  {
      // here i want to get the Total Size of the currentDirectory and freeSize
      // i want them in Bytes
  }

I searched on google but I don’t have enough experience to find out the correct tag

Thnx

+3
source share
2 answers

, , , :

FolderInfo = (CONADefinitions.CONAPI_FOLDER_INFO)Marshal.PtrToStructure(Buffer, typeof(CONADefinitions.CONAPI_FOLDER_INFO));
if (FolderInfo.pstrName[0].ToString() != "C" && level == 0) 
{
  ulong FreeBytesAvailable;
  ulong TotalNumberOfBytes;
  ulong TotalNumberOfFreeBytes;

  bool success = GetDiskFreeSpaceEx("C:\\", out FreeBytesAvailable, out TotalNumberOfBytes,out TotalNumberOfFreeBytes);

  if (!success)
    throw new System.ComponentModel.Win32Exception();
}

, ( ):

[DllImport("coredll.dll", SetLastError=true, CharSet=CharSet.Auto, EntryPoint="GetDiskFreeSpaceEx")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool InternalGetDiskFreeSpaceEx(string lpDirectoryName, out ulong lpFreeBytesAvailable, out ulong lpTotalNumberOfBytes, out ulong lpTotalNumberOfFreeBytes);

static GetDiskFreeSpaceEx(string directoryName, out ulong freeBytesAvailable, out ulong totalNumberOfBytes, out totalNumberOfFreeBytes);
{
  if (!GetDiskFreeSpaceEx(directoryName, out freeBytesAvailable, out totalNumberOfBytes, out totalNumberOfFreeBytes))
    throw new System.ComponentModel.Win32Exception();
}

:

FolderInfo = (CONADefinitions.CONAPI_FOLDER_INFO)Marshal.PtrToStructure(Buffer, typeof(CONADefinitions.CONAPI_FOLDER_INFO));
if (FolderInfo.pstrName[0].ToString() != "C" && level == 0) 
{
  ulong FreeBytesAvailable;
  ulong TotalNumberOfBytes;
  ulong TotalNumberOfFreeBytes;

  GetDiskFreeSpaceEx("C:\\", out FreeBytesAvailable, out TotalNumberOfBytes,out TotalNumberOfFreeBytes);
}
+2

, , . , , .

P/Invoke. . . - :

public class MyClass
{
  [DllImport("coredll.dll", SetLastError=true, CharSet=CharSet.Auto)]
  [return: MarshalAs(UnmanagedType.Bool)]
  internal static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
    out ulong lpFreeBytesAvailable,
    out ulong lpTotalNumberOfBytes,
    out ulong lpTotalNumberOfFreeBytes);
}

( , ):

ulong GetDiskSize(string volumeName)
{
  ulong avail;
  ulong total;
  ulong totalfree;

  MyClass.GetDiskFreeSpaceEx(volumeName, out avail, out total, out totalFree);

  return total;
  // return others as desired
}

:

ulong diskSize = GetDiskSize("\\Storage Card");
0

All Articles