Posted By: dot_tom | Oct 22nd, 2006 @ 10:14 AM
page 1 of 1
Comments: 10 | Views: 8804
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.
i think this is what you are looking for.
figuerres
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...
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?

DCMonkey
DCMonkey
Monkey see, monkey do, monkey will destroy you!
You could also use WMI and the classes from the System.Management namespace.

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

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!!!!

PerfectPhase
PerfectPhase
"This is not war, this is pest control!" - Dalek to Cyberman
Load the powershell runtime and issue a powershell command to map the drive Smiley
DCMonkey
DCMonkey
Monkey see, monkey do, monkey will destroy you!
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

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

page 1 of 1
Comments: 10 | Views: 8804
Microsoft Communities