469,607 Members | 1,986 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,607 developers. It's quick & easy.

Space available on remote computer

VB.NET

Can anyone tell me how I can get the Drive Space information from a remote
computer on the same domain and physical network? Primarily I need the Total
disk space and space used or free (or both).

--
Lamont - OKC Developer
Nov 22 '05 #1
3 29381
Hi Lamont,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to get disk information from a
remote computer. If there is any misunderstanding, please feel free to let
me know.

We can use WMI to get this information remotely. Here I found a code
snippet that can caculate the total free disk space available on a remote
machine. It is in C# and you can easily convert it to VB.NET. Also, you can
make some changes to it to meet your needs.

private void CalculateFreeUsed(string srvname)
{
try
{
// Connection credentials to the remote computer -
// not needed if the logged in account has access
ConnectionOptions oConn = new ConnectionOptions();

// oConn.Username = "JohnDoe";
// oConn.Password = "JohnsPass";
string strNameSpace = @"\\";

if (srvname != "")
strNameSpace += srvname;
else
strNameSpace += ".";

strNameSpace += @"\root\cimv2";

System.Management.ManagementScope oMs = new
System.Management.ManagementScope(strNameSpace, oConn);

//get Fixed disk stats

System.Management.ObjectQuery oQuery = new
System.Management.ObjectQuery("select FreeSpace,Size,Name from
Win32_LogicalDisk where DriveType=3");

//Execute the query
ManagementObjectSearcher oSearcher = new
ManagementObjectSearcher(oMs,oQuery);

//Get the results
ManagementObjectCollection oReturnCollection = oSearcher.Get();

//loop through found drives and write out info

foreach( ManagementObject oReturn in oReturnCollection )
{
// Disk name
//Console.WriteLine("Name : " + oReturn["Name"].ToString());
// Free Space in bytes
strFreespace = oReturn["FreeSpace"].ToString();
D_Freespace = D_Freespace + System.Convert.ToDouble(strFreespace);
// Size in bytes
strTotalspace = oReturn["Size"].ToString();
D_Totalspace = D_Totalspace + System.Convert.ToDouble(strTotalspace);
}
}
catch
{
MessageBox.Show("Failed to obtain Server Information. The node you are
trying to scan can be a Filer or a node which you don't have administrative
priviledges. Please use the UNC convention to scan the shared folder in the
server","Server Error",MessageBoxButtons.OK, MessageBoxIcon.Error) ;
}
}

Please check the following link for more information on this snippet.

http://groups.google.com/group/micro...es.vb/browse_f
rm/thread/8cfed7c8d264ded/ebaf526cf2817346?lnk=st&q=disk+information+remote+
computer+wmi&rnum=7&hl=zh-CN#ebaf526cf2817346

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 22 '05 #2
You are correct, and I think this will do the job.

Thank you.
--
Lamont - OKC Developer
"Kevin Yu [MSFT]" wrote:
Hi Lamont,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to get disk information from a
remote computer. If there is any misunderstanding, please feel free to let
me know.

We can use WMI to get this information remotely. Here I found a code
snippet that can caculate the total free disk space available on a remote
machine. It is in C# and you can easily convert it to VB.NET. Also, you can
make some changes to it to meet your needs.

private void CalculateFreeUsed(string srvname)
{
try
{
// Connection credentials to the remote computer -
// not needed if the logged in account has access
ConnectionOptions oConn = new ConnectionOptions();

// oConn.Username = "JohnDoe";
// oConn.Password = "JohnsPass";
string strNameSpace = @"\\";

if (srvname != "")
strNameSpace += srvname;
else
strNameSpace += ".";

strNameSpace += @"\root\cimv2";

System.Management.ManagementScope oMs = new
System.Management.ManagementScope(strNameSpace, oConn);

//get Fixed disk stats

System.Management.ObjectQuery oQuery = new
System.Management.ObjectQuery("select FreeSpace,Size,Name from
Win32_LogicalDisk where DriveType=3");

//Execute the query
ManagementObjectSearcher oSearcher = new
ManagementObjectSearcher(oMs,oQuery);

//Get the results
ManagementObjectCollection oReturnCollection = oSearcher.Get();

//loop through found drives and write out info

foreach( ManagementObject oReturn in oReturnCollection )
{
// Disk name
//Console.WriteLine("Name : " + oReturn["Name"].ToString());
// Free Space in bytes
strFreespace = oReturn["FreeSpace"].ToString();
D_Freespace = D_Freespace + System.Convert.ToDouble(strFreespace);
// Size in bytes
strTotalspace = oReturn["Size"].ToString();
D_Totalspace = D_Totalspace + System.Convert.ToDouble(strTotalspace);
}
}
catch
{
MessageBox.Show("Failed to obtain Server Information. The node you are
trying to scan can be a Filer or a node which you don't have administrative
priviledges. Please use the UNC convention to scan the shared folder in the
server","Server Error",MessageBoxButtons.OK, MessageBoxIcon.Error) ;
}
}

Please check the following link for more information on this snippet.

http://groups.google.com/group/micro...es.vb/browse_f
rm/thread/8cfed7c8d264ded/ebaf526cf2817346?lnk=st&q=disk+information+remote+
computer+wmi&rnum=7&hl=zh-CN#ebaf526cf2817346

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 22 '05 #3
You're welcome, Lamont.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Lamont | last post: by
1 post views Thread by Bhavya Shah | last post: by
4 posts views Thread by Von Thep via DotNetMonster.com | last post: by
reply views Thread by py | last post: by
reply views Thread by Zebadunc | last post: by
5 posts views Thread by Sin Jeong-hun | last post: by
reply views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.