获取远程计算机上磁盘的剩余空间,所要获取的远程计算机上的磁盘可能是共享的,也可能不是共享的。如果是共享的就好办多了,你可以不需要太多的权限就可以获取你想要的东西。

                ///   < summary >
                ///   获得UNC路径所指向文件夹所在磁盘的的剩余空间大小----需要
                ///   < /summary >
                ///   参数为UNCPath和路径所在的磁盘
                ///   < param name = " UNCPath " > < /param >
                ///   < returns > < /returns >
                private   void   btGetRemoteFolderFreeSpace_Click ( object   sender,   EventArgs   e )
                {
                     
10                         string   ip   =   textBoxSrcPath . Text;
11                         string   disksrc   =   textBoxDirPath . Text;
12                         string   username   =   txtUsername . Text;
13                         string   password   =   txtPassword . Text;
14
15                         long   freesize   =   0l;
16                         long   gb   =   1024 * 1024 * 1024 ;
17                         ConnectionOptions   connectionOptions = new   ConnectionOptions ( ) ;
18                         connectionOptions . Username   =   username;
19                         connectionOptions . Password   =   password;
20                         connectionOptions . Timeout = new   TimeSpan ( 1 , 1 , 1 , 1 ) ; // 连接时间
21
22               // ManagementScope   的服务器和命名空间。
23                         string   path   =   string . Format ( " \\\\{0}\\root\\cimv2 " ,   ip ) ;
24                         // 表示管理操作的范围(命名空间),使用指定选项初始化   ManagementScope   类的、表示指定范围路径的新实例。
25                         ManagementScope   scope   =   new   ManagementScope ( path,   connectionOptions ) ;
26                         scope . Connect ( ) ;
27                         // 查询字符串,某磁盘上信息
28                         string   strQuery = string . Format(   " select   *   from   Win32_LogicalDisk   where   deviceid='{0}' " ,disksrc ) ;
29
30                         ObjectQuery   query = new   ObjectQuery ( strQuery ) ;
31                         // 查询ManagementObjectCollection返回结果集
32                         ManagementObjectSearcher   searcher   =   new   ManagementObjectSearcher ( scope,   query ) ;
33                         foreach   ( ManagementObject   m   in   searcher . Get ( ) )
34                         {
35                                 if   ( m [ " Name " ] . ToString ( ) = = disksrc )
36                                 { // 通过m["属性名"]
37                                         freesize   =   Convert . ToInt64 ( m [ " FreeSpace " ] ) / gb;
38                                 }
39                         }
40                         MessageBox . Show ( " 磁盘 " + disksrc + " 的可用空间为 " + freesize + " GB " ) ;
41
42                 }
43

其中textBoxSrcPath.Text是值远程计算机的Ip地址,当然了,你需要提供计算机的用户名和密码,如果该磁盘整个都共享,你甚至都不需要用户名和密码,上面的例子是磁盘的某个文件夹是共享 的,但是该磁盘不共享,如果该磁盘是共享的就可以跟据unc路径想获取本地磁盘剩余空间一样获取该共享磁盘的剩余空间了。