Complete C# Console program:
----------------------------
using System;
using System.Text;
using System.Reflection;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.Remoting;
using System.Security.Principal;
using System.Text.RegularExpressions;
using System.Threading;
using System.IO;
using System.Xml;
using System.Management;
namespace NetworkDrives
{
static class Program
{
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
try
{
int NetworkDriveType = 4;
bool Flag = false;
WqlObjectQuery objectQuery = new WqlObjectQuery("select DriveType,DeviceID,ProviderName from Win32_LogicalDisk");
ManagementScope scope = new ManagementScope(@"\\.\root\CIMV2");
scope.Options.Impersonation = ImpersonationLevel.Impersonate;
scope.Options.EnablePrivileges = true;
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, objectQuery);
foreach (ManagementObject share in searcher.Get())
{
int driveType = Convert.ToInt32(share["DriveType"]);
if (driveType == NetworkDriveType)
{
object objName = share["DeviceID"];
object objPath = share["ProviderName"];
if (null != objName && null != objPath)
{
Flag = true;
Console.WriteLine(objName.ToString() + " " + objPath.ToString());
}
}
}
if (!Flag)
Console.WriteLine("No mapped network drives found.");
Console.Read();
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.Message);
}
}
private static void netuse(string FQDNPath)
{
Process DESProcess = new Process();
try
{
DESProcess.StartInfo.FileName = "cmd.exe";
DESProcess.StartInfo.Arguments = " /c net use * " + FQDNPath;
DESProcess.StartInfo.UseShellExecute = false;
DESProcess.StartInfo.CreateNoWindow = true;
DESProcess.StartInfo.LoadUserProfile = true;
DESProcess.StartInfo.RedirectStandardError = true;
DESProcess.StartInfo.RedirectStandardInput = true;
DESProcess.StartInfo.RedirectStandardOutput = true;
DESProcess.Start();
string output = string.Empty;
while (DESProcess.StandardOutput.Peek() > -1)
{
output = output + ((char)DESProcess.StandardOutput.Read()).ToString();
}
Console.WriteLine(output);
}
catch (Exception ex)
{
Console.WriteLine("Exception occured while mapping network drive. " + ex.Message);
}
}
}
}
Regards,
Megha