JustBeingHelpful
JustBeingHelpful

Reputation: 18980

UNC path can be mapped from command line but not from .NET Console application

How can I make the .NET code map the Z: drive to the UNC path for account SVNdatamgmt?

I'm trying to map a local drive to a network UNC path within a .NET console application. The code seems to work from the command line for both the service account (#2 and #3) and with my credentials (#4). But when running from the Console application using .NET source code, the service account doesn't work (#5), but my credentials do work (#6).

Last night, I noticed that I got an error (#1). After waiting 30 minutes, it worked. So you can ignore #1. I thought I'd mention it, in case it gives a clue as to what is happening.

The console application is running as an administrator on a Windows Server 2008 box. Both SVCdatamgmt and macgyver are administrators on this machine. The commands are also being run on this same machine.

========================================================================
1.) This didn’t work last night:

C:>net use z: \\uwhc-sas2\SASHIMC /USER:SVCdatamgmt thepassword

System error 1909 has occurred.
The referenced account is currently locked out and may not be logged on to.

========================================================================
2.) Waited 30 minutes and then this worked (without domain):

C:>net use z: \\uwhc-sas2\SASHIMC /USER:SVCdatamgmt thepassword

The command completed successfully.

========================================================================
3.) This also works (with domain):

C:>net use z: \\uwhc-sas2\SASHIMC /USER:UWHIS\SVCdatamgmt thepassword

The command completed successfully.

========================================================================
4.) This also works with my credentials:

C:>net use z: \\uwhc-sas2\SASHIMC /USER:macgyver thepassword

The command completed successfully.

========================================================================
5.) .NET code that maps drive. SVCdatamgmt credentials do not work.

public static void MapNetworkDriveToUNC()
{
    var command = @"net use " + mapDrive + " " + uncPath + " " + uncUser + " " + uncPass;
    ExecuteCommand(command, 10000);
}

public static void UnmapNetworkDriveToUNC()
{
    var command = "net use " + mapDrive + " /delete";
    ExecuteCommand(command, 5000);
}

========================================================================
6.) .NET code that maps drive. My credentials work (macgyver)

-- same code as #5 --

========================================================================
FYI: before running each command above, I have to disconnect (unmap) the drive using this code...

C:\>net use z: /delete

z: was deleted successfully.

Upvotes: 0

Views: 6956

Answers (1)

user1701450
user1701450

Reputation: 72

I was looking for the command to map UNC Path to Network Drive from C# and I implemented this and works great. Its too late but may be helpful to someone in future:

  public static bool MapDrivetoUNC(string DriveName, string Path)
  {
     try
     {
        bool isMapped = true;

        System.Diagnostics.Process p = new System.Diagnostics.Process();
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.CreateNoWindow = true;
        //p.StartInfo.RedirectStandardError = true;
        //p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.FileName = "net.exe";
        p.StartInfo.Arguments = " use " + DriveName + ": " + '"' + Path + '"'; 
        p.Start();
        p.WaitForExit();

        //str errMsg = p.StandardError.ReadToEnd();
        // similar for erroutput .. 
        isMapped = true;
     }
     catch(Exception ex)
     { 
         Utility.logError(ex);
         isMapped = false;
     }
     return isMapped;
  }

Upvotes: 1

Related Questions