4

I know that, AvailableFreeSpace is possible to use for local drives such as "C:/", "D:/" etc. It also works on network drives.

But now my question is:

Is it possible to view the AvailableFreeSpace of a "Folder" on another IP? I connect to the local drives with this code:

System.IO.DriveInfo _DriveInfo = new DriveInfo(SaveLocation);

When "SaveLocation" is a local drive like "C:\Temp\Folder", than it works fine.

But when there is an IP inside "SaveLocation" it doesn't work. SaveLocation looks like this in that case: "192.168.200.10\c\Data"

This doesn't work and that is the reason for my question. The Exceptionmessage is: {"Object must be a root directory (\"C:\\") or a drive letter (\"C\")."}

I hope you can help me.

Evosoul
  • 197
  • 1
  • 12
  • What if you add shared location as a mapped drive and check its free space? – Hamid Pourjam Oct 21 '16 at 07:08
  • @dotctor mapped drives works, but I only get the string (I don't have influence about it). The string is an IP most of the time and I can't add each IP I get as mapped drive. The programitically mapping is to long because I have to create it each time I get the string and dissolve it after the save is finished. ( I have to that because there is a possibility that i can get a lot of different IP's) – Evosoul Oct 21 '16 at 07:27
  • WMI is a little trickier but I think you can ask for the available space using it. Have a look at this question http://stackoverflow.com/questions/1412395/how-can-i-check-for-available-disk-space. It's using localhost but you could extract the IP. The only thing I know cannot be done is to convert a UNC to the actual local path in the remote machine, unless you have administrator rights, so if remote machines have more than one drive it might not work. – jorgonor Oct 21 '16 at 07:47

1 Answers1

4

As seen in Get available disk free space for a given path on Windows :

Use the winapi function GetDiskFreeSpaceEx to determine free space on a UNC (network) path. For example, create a new VS Project called FreeSpace and paste this as Program.cs:

    using System;
    using System.Runtime.InteropServices;

    namespace FreeSpace
    {
        class Program
        {
            [DllImport("kernel32.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);

            static void Main(string[] args)
            {
                ulong FreeBytesAvailable;
                ulong TotalNumberOfBytes;
                ulong TotalNumberOfFreeBytes;

                bool success = GetDiskFreeSpaceEx(@"\\NETSHARE\folder",
                                              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);
                Console.ReadKey();
            }
        }
    }

As you can see, this is the exact same code as in the Question linked above, just factored into a class plus the correct using directives to compile without error. All credits go to https://stackoverflow.com/users/995926/rekire

WMI doesn't seem to handle free space on network shares. But for local disks, Windows Management Interface is the way to go: https://msdn.microsoft.com/en-us/library/aa394592(v=vs.85).aspx

Community
  • 1
  • 1
NoMad
  • 702
  • 2
  • 11
  • 27
  • This works for Mapped Drives but not for an IP-adress without the mapping. So sadly this does not work in my case – Evosoul Oct 21 '16 at 11:32
  • Sorry this doesn't work for you. Alternatively, you could also use the GetDiskFreeSpaceEx winapi function: https://msdn.microsoft.com/de-de/library/windows/desktop/aa364937(v=vs.85).aspx See http://stackoverflow.com/questions/14465187/get-available-disk-free-space-for-a-given-path-on-windows – NoMad Oct 24 '16 at 09:24
  • The [DLLImport] variant worked! Thanks! Can you formulate an answer so I can accept it? – Evosoul Oct 25 '16 at 05:50
  • Just edited my answer with a link to the existing question. I'm not sure, isn't this a duplicate to the linked question? If so, should we flag it? – NoMad Oct 28 '16 at 11:26
  • Not necessarily. He wants a folder or a mapped drive. I want to connect to a folder via IP-Address and not mapped or local drives. I change my question, so it could be clear – Evosoul Nov 02 '16 at 06:54