Print all mapped network drives / Map the specified network drive to local computer

Megha P posted at 12-Oct-08 09:28
/* This program's main function will print all the mapped network drive on local computer.

The netuse function can map the specified network drive. Please give FQDN path for netwrok drive to be mapped like
\\domain\site\folder\.

Please contact me by replying to this thread if you find any difficulties or if you have 
any questions about this.

*/

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

Click here to sign in and reply. You could earn money via our $500 contest just for being helpful.
  Print all mapped network drives / Map the specified network drive to local computer - Megha P  12-Oct-08 09:28 9:28:46 AM
      re - Lalji Mer  13-Oct-08 04:37 4:37:18 AM
View Posts