Search code examples
c#network-programmingwindows-servicesnetwork-protocolsmappings

Map shared folder on another domain using net.exe in a c# Windows Service application


I am trying to map a network drive on to the server inside a windows service written in c#. I tried using net.exe with below code

        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 " + DriveLetter + Path + " " + Password + " /user:" + Username;
        p.Start();
        p.WaitForExit();

the arguments basically translate to " use X: \\192.45.xx.xxx\sharename "xxxx" /user: domainname\username"

i get a "network name not found" error when this executes. The server i am trying to access is on a different domain than the one my app is sitting on, which is why i think i get that error.

Does anyone know how i can get past this and map a shared folder from another domain on to my local computer?

I'd greatly appreciate any help

Thanks Karthik


Solution

  • the arguments basically translate to " use X: \192.45.xx.xxx\sharename "xxxx" /user: domainname\username"

    Assuming everything else is correct, this error is caused by specifying one backslash before the IP address -- you need 2 backslashes.

    enter image description here

    update

    In response to your comments, clearly you are authenticated to access resources this other domain. Unless it is wide open, it is unlikely that the Windows Service, which is probably running under a system account, can access those resources.

    As a troubleshooting step, change your service to run under your account, and see if that makes a difference.