C # How to obtain the remaining space on the remote disk

Source: Internet
Author: User

 

Obtain the remaining disk space on the remote computer. The disk on the remote computer to be obtained may be shared or not shared. If it is shared, you can get what you want without having too many permissions.

1 /// <summary>

2 // obtain the remaining space on the disk where the UNC path points to the folder ---- required

3 /// </summary>

4 // The parameter is UNCPath and the disk where the path is located

5 /// <param name = "UNCPath"> </param>

6 /// <returns> </returns>

7 private void btGetRemoteFolderFreeSpace_Click (object sender, EventArgs e)

8 {

9

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 (,); // connection time

21

22 // the server and namespace of ManagementScope.

23 string path = string. Format ("\\\\{ 0 }\\ root \ cimv2", ip );

24 // indicates the management operation range (namespace). Use the specified option to initialize a new instance of the ManagementScope class, indicating the specified range path.

25 ManagementScope scope = new ManagementScope (path, connectionOptions );

26 scope. Connect ();

27 // query string, information on a disk

28 string strQuery = string. Format ("select * from Win32_LogicalDisk where deviceid = '{0}'", disksrc );

29

30 ObjectQuery query = new ObjectQuery (strQuery );

31 // query the returned result set of ManagementObjectCollection

32 ManagementObjectSearcher searcher = new ManagementObjectSearcher (scope, query );

33 foreach (ManagementObject m in searcher. Get ())

34 {

35 if (m ["Name"]. ToString () = disksrc)

36 {// use m ["attribute name"]

37 freesize = Convert. ToInt64 (m ["FreeSpace"])/gb;

38}

39}

40 MessageBox. Show (the available space for "disk" + disksrc + "is" + freesize + "GB ");

41

42}

43

TextBoxSrcPath. text is the IP address of the remote computer. Of course, you need to provide the user name and password of the computer. If the entire disk is shared, you do not even need the user name and password, in the above example, a folder on the disk is shared, but the disk is not shared, if the disk is shared, you can obtain the remaining space of the shared disk like the space remaining on the local disk in the unc path.

 

This article is from the "HDDevTeam" blog

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.