0

我们有一个服务器,它为我们组织中的每个用户提供一个网络共享,以将他们的文件备份到。所有共享都物理地位于同一文件夹中的服务器上。即 D:\UserArchives\user1$,d:\UserArchives\user2$。所有股票都以隐藏的美元符号为后缀。

我正在尝试使用 c# 提取每个用户在各自共享中可用的可用空间量。

我正在使用此处选择的答案枚举共享:使用 C# 枚举网络共享

我一直在尝试使用 GetDiskFreeSpaceEx 从这里提取可用空间:http: //social.msdn.microsoft.com/Forums/ar-SA/csharpgeneral/thread/b7db7ec7-34a5-4ca6-89e7-947190c4e043

如果我以 user1 身份运行我的应用程序,它会为他们的共享提取适量的可用空间,并且我会遇到安全异常,因为他们无法访问 user2 的共享,反之亦然。这是意料之中的。当我将我的应用程序作为可以访问这两个共享的管理帐户运行时,我会取回 user1 + user2 的可用空间量。

这也很有意义,因为 GetDiskFreeSpaceEx 的文档指出:

 "Retrieves information about the amount of space that is available on a disk volume, which is the total amount of space, the total amount
 of free space, and the total amount of free space available to the
 user that is associated with the calling thread."

我的问题是如何通过网络在 C# 中实现这一点。我不想要服务器本地的解决方案。

到目前为止我的代码:

        static void Main(string[] args)
        {
            string server = "filesvr1";
            try
            {
            //Impersonator is a class for impersonating a different windows account
                using (new Impersonator("UserName", "Domain", "Password"))
                {
                    GetUserShareInfo(server);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR: " + ex.Message);
            }
        }
        static void GetUserShareInfo(string server) 
        {
            ShareCollection shi;
            if (server != null && server.Trim().Length > 0)
            {
                Console.WriteLine("\nShares on {0}:", server);
                shi = ShareCollection.GetShares(server);
                if (shi != null) 
                {
                    foreach(Share si in shi) 
                    {
                        // If you don't have permissions to the share you will get security exceptions.
                        if (si.IsFileSystem) 
                        {
                            string userName = si.NetName.Substring(0, si.NetName.Length - 1);
                            try
                            {
                                //Network Share Size
                                Console.WriteLine(FreeSpace("\\\\" + server + "\\" + si.Root.Name));
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(userName + " - " + ex.Message);
                            }
                        }
                    }
                }
                else
                    Console.WriteLine("Unable to enumerate the shares on {0}.\n"
                        + "Make sure the machine exists, and that you have permission to access it.", server);
            }
        }

   public static long FreeSpace(string folderName)
        {
            if (string.IsNullOrEmpty(folderName))
            {
                throw new ArgumentNullException("folderName");
            }

            if (!folderName.EndsWith("\\"))
            {
                folderName += '\\';
            }

            long free = 0, dummy1 = 0, dummy2 = 0;

            if (GetDiskFreeSpaceEx(folderName, ref free, ref dummy1, ref dummy2))
            {
                return free;
            }
            else
            {
                return -1;
            }
        }

        [SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage"), SuppressUnmanagedCodeSecurity]
        [DllImport("Kernel32", SetLastError = true, CharSet = CharSet.Auto)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool GetDiskFreeSpaceEx
        (
            string lpszPath,                    // Must name a folder, must end with '\'.
            ref long lpFreeBytesAvailable,
            ref long lpTotalNumberOfBytes,
            ref long lpTotalNumberOfFreeBytes
        );
4

0 回答 0