Checking for Mapped Drive

Trying to check for drive mapping verification using the wshNet method

Page 1 of 1

3 Replies - 10788 Views - Last Post: 18 December 2009 - 04:24 PM Rate Topic: -----

#1 WECOTEch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-December 09

Checking for Mapped Drive

Post icon  Posted 17 December 2009 - 01:21 PM

Hello,


I am having problems verifying that the drive is mapped.

The code I have is this:

For the Public Class I have this:
Dim wshNet As New IWshRuntimeLibrary.WshNetwork


wshNet.MapNetworkDrive("Z:", "\\10.21.2.218\Share", True, "wecon\sophosuser", "soweco")

To remove the drive I am using:
'wshNet.RemoveNetworkDrive ("Z:\", False, False)

What I am looking for is how to verify that the z drive is mapped, and if so then run the unmapping drive command.

When I try to use a if wshNet. anything the stupid thing won't return a true or false value

Can someone please help?
I searched all through the net to find some sort of If drive mapped = true, however nothing comes up that I can implement.

mark

Is This A Good Question/Topic? 0
  • +

Replies To: Checking for Mapped Drive

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1658
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Checking for Mapped Drive

Posted 17 December 2009 - 01:33 PM

I would use WMI (Windows Management Instrumentation) with the Win32_MappedLogicalDisk Class over the way you're trying.

Here's an example of getting all mapped drives with the technique I mentioned

	/// <summary>
	/// method for retrieving all mapped drives
	/// </summary>
	/// <returns></returns>
	public List<MappedDrive> GetMappedDriveList()
	{
		try
		{
			List<string> drives = new List<string>();
			ConnectionOptions options = new ConnectionOptions();
			options.Impersonation = ImpersonationLevel.Impersonate;
			ManagementPath path = new ManagementPath(@"\\" + Environment.MachineName + @"\root\cimv2");

			ObjectQuery query = new ObjectQuery("select * from Win32_MappedLogicalDisk");

			ManagementScope scope = new ManagementScope(path, options);
			ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

			foreach (ManagementObject obj in searcher.Get())
				drives.Add(obj.Properties["Name"].Value.ToString());

			return drives;
		}
		catch (Exception ex)
		{
		   MessageBox.Show(string.Format("Error mapping drive: {0}", ex.Message));
			return null;
		}

	}



For disconnecting a mapped drive look up the WNetCancelConnection2A Function
Was This Post Helpful? 0
  • +
  • -

#3 WECOTEch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-December 09

Re: Checking for Mapped Drive

Posted 18 December 2009 - 03:38 PM

View PostPsychoCoder, on 17 Dec, 2009 - 12:33 PM, said:

I would use WMI (Windows Management Instrumentation) with the Win32_MappedLogicalDisk Class over the way you're trying.

Here's an example of getting all mapped drives with the technique I mentioned

	/// <summary>
	/// method for retrieving all mapped drives
	/// </summary>
	/// <returns></returns>
	public List<MappedDrive> GetMappedDriveList()
	{
		try
		{
			List<string> drives = new List<string>();
			ConnectionOptions options = new ConnectionOptions();
			options.Impersonation = ImpersonationLevel.Impersonate;
			ManagementPath path = new ManagementPath(@"\\" + Environment.MachineName + @"\root\cimv2");

			ObjectQuery query = new ObjectQuery("select * from Win32_MappedLogicalDisk");

			ManagementScope scope = new ManagementScope(path, options);
			ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

			foreach (ManagementObject obj in searcher.Get())
				drives.Add(obj.Properties["Name"].Value.ToString());

			return drives;
		}
		catch (Exception ex)
		{
		   MessageBox.Show(string.Format("Error mapping drive: {0}", ex.Message));
			return null;
		}

	}



For disconnecting a mapped drive look up the WNetCancelConnection2A Function


Ahh.. I am sure that would work if I was doing Dot Net in C++, however I am using VB Dot Net
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1658
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Checking for Mapped Drive

Posted 18 December 2009 - 04:24 PM

That's good since that's not C++ code :)

It's C# code and you can follow the same logic (even class names) to do this in VB.NET. I'm not going to do all your work for you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1