Tech Off Thread

13 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

The best way to effect "net use" from C#?

Back to Forum: Tech Off
  • User profile image
    tomkirbygre​en

    What's the best way to effect a "net use" from C#? I need to be able to establish named drives, passing the user name and password. AndAlso of course remove said mappings. In other words I'd like to be able to carry out variations on a theme of:

       net use q: \\SomeServer\SomeShare foo /user:bar

    and

       net use /d q:

    One way of doing this might be to use System.Diagnostics.Process and redirect the stdout and stderr so I can parse any output to delivery decent structured error messages that my code can act upon. However that seems a little clunky. I'd really rather PInvoke a set of APIs than "shell out". Anyone got any thoughts?

    Cheers - tom.

  • User profile image
    devnull

    i think this is what you are looking for.

  • User profile image
    figuerres

    you may also want to look at:
    http://www.codeguru.com/csharp/csharp/cs_network/windowsservices/article.php/c12357/

    which has a sample zip file with the api calls in a.net class

    also your best buddy in windows / .net is www.pinvoke.net

    look at:

    http://www.pinvoke.net/default.aspx/mpr.WNetAddConnection3
    and the other mpr bits...

  • User profile image
    tomkirbygre​en

    Doh. Of course <blush>. Many thanks for the pointer! Smiley

  • User profile image
    Arun Prasad

    To execute shell command from c#, save the commands within a batch file (.bat) extension. You can create a batch file using Notepad application and saving the file like <your-preferred-name.bat> (example: demo123.bat).

    After creating the batch file, save the commands within the file like 
    net use a: \\192.168.1.1\SharedFilder myPassword /user:MyUserName.
    A complete followup of "net use" command can be found in www.outsmartsoftware.com or technet.Microsoft.com

    Once you are settled with the batch file, invoke it from the C# code:
        System.Diagnostics.Process objProcess = new Process();
        objProcess.StartInfo.FileName = Server.MapPath("testfiles/netcmd.bat");
        objProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;    // to hide the command window popping up
        objProcess.Start();
        objProcess.WaitForExit();    // Gives time for the process to complete operation.
        // After code is executed, call the dispose() method
        objProcess.Dispose();

    and there we go!!!!

    for clarification, please feel free to reply to this post.
    With thanks,
    Arun Prasad.K.R
    arun85prasad@live.com

  • User profile image
    cheong

    Arun Prasad said:
    To execute shell command from c#, save the commands within a batch file (.bat) extension. You can create a batch file using Notepad application and saving the file like <your-preferred-name.bat> (example: demo123.bat).

    After creating the batch file, save the commands within the file like 
    net use a: \\192.168.1.1\SharedFilder myPassword /user:MyUserName.
    A complete followup of "net use" command can be found in www.outsmartsoftware.com or technet.Microsoft.com

    Once you are settled with the batch file, invoke it from the C# code:
        System.Diagnostics.Process objProcess = new Process();
        objProcess.StartInfo.FileName = Server.MapPath("testfiles/netcmd.bat");
        objProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;    // to hide the command window popping up
        objProcess.Start();
        objProcess.WaitForExit();    // Gives time for the process to complete operation.
        // After code is executed, call the dispose() method
        objProcess.Dispose();

    and there we go!!!!

    for clarification, please feel free to reply to this post.
    With thanks,
    Arun Prasad.K.R
    arun85prasad@live.com

    Why suggest a method that the origional poster mentioned he knows, but don't wish to do if have other choice?

    Recent Achievement unlocked: Code Avenger Tier 4/6: You see dead program. A lot!
    Last modified
  • User profile image
    DCMonkey

    You could also use WMI and the classes from the System.Management namespace.

  • User profile image
    figuerres

    cheong said:
    Arun Prasad said:
    *snip*

    Why suggest a method that the origional poster mentioned he knows, but don't wish to do if have other choice?

    Even better is why did he bring back a dead thread from 2 YEARS AGO?

  • User profile image
    Arun Prasad

    DCMonkey said:
    You could also use WMI and the classes from the System.Management namespace.

    Hi DCM,
    I do not know much about WMI and is there a better way out with System.Management namespaces? These two are relatively new to me, and may be can you show me the way to get there plz?

    About bridging the two years of dead interval is that may be, at some point of time, this could help out few new and uninitated by getting them right on track, Just like "dot_tom" showed me the way!!!!

  • User profile image
    PerfectPhase

    Arun Prasad said:
    DCMonkey said:
    *snip*
    Hi DCM,
    I do not know much about WMI and is there a better way out with System.Management namespaces? These two are relatively new to me, and may be can you show me the way to get there plz?

    About bridging the two years of dead interval is that may be, at some point of time, this could help out few new and uninitated by getting them right on track, Just like "dot_tom" showed me the way!!!!

    Load the powershell runtime and issue a powershell command to map the drive Smiley

  • User profile image
    DCMonkey

    Arun Prasad said:
    DCMonkey said:
    *snip*
    Hi DCM,
    I do not know much about WMI and is there a better way out with System.Management namespaces? These two are relatively new to me, and may be can you show me the way to get there plz?

    About bridging the two years of dead interval is that may be, at some point of time, this could help out few new and uninitated by getting them right on track, Just like "dot_tom" showed me the way!!!!

    I haven't actually used it. I just knew the capability was there.

    Here's some docs on WMI from a .Net perspective:
    http://msdn.microsoft.com/en-us/library/aa720264(VS.71).aspx">http://msdn.microsoft.com/en-us/library/aa720264(VS.71).aspx

    There are a bunch of examples of dealing with shares via WMI out there. Here is one:
    http://www.mvps.org/emorcillo/en/code/grl/share.shtml

  • User profile image
    Demonforce

    Arun Prasad said:
    To execute shell command from c#, save the commands within a batch file (.bat) extension. You can create a batch file using Notepad application and saving the file like <your-preferred-name.bat> (example: demo123.bat).

    After creating the batch file, save the commands within the file like 
    net use a: \\192.168.1.1\SharedFilder myPassword /user:MyUserName.
    A complete followup of "net use" command can be found in www.outsmartsoftware.com or technet.Microsoft.com

    Once you are settled with the batch file, invoke it from the C# code:
        System.Diagnostics.Process objProcess = new Process();
        objProcess.StartInfo.FileName = Server.MapPath("testfiles/netcmd.bat");
        objProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;    // to hide the command window popping up
        objProcess.Start();
        objProcess.WaitForExit();    // Gives time for the process to complete operation.
        // After code is executed, call the dispose() method
        objProcess.Dispose();

    and there we go!!!!

    for clarification, please feel free to reply to this post.
    With thanks,
    Arun Prasad.K.R
    arun85prasad@live.com

    Why use the bat file at all? it is a big risk because the password is stored in there!.

     

    Most simple way to do it is:

    ProcessStartInfo PInfo;
    Process P;
    PInfo= new ProcessStartInfo("cmd", @"/c net use \\server password /user:username");
    PInfo.CreateNoWindow = false; //nowindow
    PInfo.UseShellExecute = true; //use shell
    P = Process.Start(PInfo);
    P.WaitForExit(5000); //give it some time to finish
    P.Close();

     

    This way without knowing the sourcecode, they wont know the password. Only thingy tough is that you cannot just change the password..

     

  • User profile image
    Sven Groot

    Demonforce said:
    Arun Prasad said:
    *snip*

    Why use the bat file at all? it is a big risk because the password is stored in there!.

     

    Most simple way to do it is:

    ProcessStartInfo PInfo;
    Process P;
    PInfo= new ProcessStartInfo("cmd", @"/c net use \\server password /user:username");
    PInfo.CreateNoWindow = false; //nowindow
    PInfo.UseShellExecute = true; //use shell
    P = Process.Start(PInfo);
    P.WaitForExit(5000); //give it some time to finish
    P.Close();

     

    This way without knowing the sourcecode, they wont know the password. Only thingy tough is that you cannot just change the password..

     

    Necrothread, but can't resist...

     

    Sorry, but strings are stored in plain text inside the executable's data section. Anyone with a hex editor can read them. And tools like ildasm and Reflector make this even easier with .Net. Embedding the string with the password in the executable is not an effective way to secure it.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.