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.
-
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... -
Doh. Of course <blush>. Many thanks for the pointer!

-
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 -
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.comWhy suggest a method that the origional poster mentioned he knows, but don't wish to do if have other choice?
-
You could also use WMI and the classes from the System.Management namespace.
-
Even better is why did he bring back a dead thread from 2 YEARS AGO?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?
-
Hi DCM,DCMonkey said:You could also use WMI and the classes from the System.Management namespace.
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 driveArun Prasad said:
Hi DCM,DCMonkey said:*snip*
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.Arun Prasad said:
Hi DCM,DCMonkey said:*snip*
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!!!!
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
-
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.comWhy 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..
-
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.
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.