Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
So I wrote Some Code..
I implemented the "net use command nicely"

=======

System.Diagnostics.Process login;
login = new System.Diagnostics.Process();

string cleardrives;
cleardrives = "/C net use * /delete /y";


string mapDriveP;
mapDriveP = "/C net use t: \\\\SERVER\\multimedia /user:"+ textBox1.Text + " " + textBox2.Text;
string mapDriveS;
mapDriveS = "/C net use s: \\\\SERVER\\temp /user:" + textBox1.Text + " " + textBox2.Text;
string mapDriveU;
mapDriveU = "/C net use u: \\\\SERVER\\USER /user:" + textBox1.Text + " " + textBox2.Text;


MIDL
System.Diagnostics.Process.Start("CMD.exe", cleardrives);
System.Threading.Thread.Sleep(4000);
System.Diagnostics.Process.Start("CMD.exe", mapDriveP);
System.Threading.Thread.Sleep(2000);
System.Diagnostics.Process.Start("CMD.exe", mapDriveS);
System.Threading.Thread.Sleep(2000);
System.Diagnostics.Process.Start("CMD.exe", mapDriveU);
login.Close();


=================

This works.. the problem is sometimes it takes some time for the first drive to map
and I just want to execute all the map commands in one system window.

I thought about implementing a .bat file in the code but I got stuck on the insertion of the texbox fields (which are the username and password for the net use
command)

Any help would be gratly appreciated.

Again the question HOW DO I START THE MAPDRIVEP, MAPDRIVES and MAPDRIVEU in one system window now I get 3 systemwindow popups..

[edit]Spurious code block removed - OriginalGriff[/edit]
Posted
Updated 30-Apr-11 22:24pm
v2

Just a note: there is not such thing as "DOS Command". If you're talking about "CMD.EXE" — this is just a regular Windows protected-more application using console and running command interpreter.

You don't need to run it in your case. You need to run "NET.EXE" itself:

C#
string cmdLine = string.Format("{0} {1} {2}", mapDriveP + mapDriveU + mapDriveS);
System.Diagnostics.Process.Start("NET.EXE", cmdLine);


If you have a problem running only one "NET.EXE" session you can consider running it one-by-one:

C#
string[] parameters = new string[] {
@"use u: \\server\multimedia /user:""indiana jones""",
@"use p: \\server\temp /user:""indiana jones""",
@"use s: \\server\user /user:""indiana jones""", };

//warning: I suspect must be /user:"Indiana Jones" (in quotation)

foreach(string cmdLineParameter in parameters) {
   System.Diagnostics.Process process = 
      System.Diagnostics.Process.Start("NET.EXE", cmdLineParameter);
   process.WaitForExit();
}


If one instance of the "NET.EXE" process does not depend on the result of another one, you can omit WaitForExit, but if you further code depends on the results, you need to wait anyway.

Another option is using batch file.
C#
System.Diagnostics.Process.Start("myBatchFile.bat");


Do not use Sleep! Do not use "CMD.EXE"!!!

—SA
 
Share this answer
 
v6
Comments
Hooki 1-May-11 23:36pm    
I'm trying to get Familiar with NET.EXE and I can't seem to figure it out.

In your code does what does the cmdLine string need to look like..
Because mapDriveP just puts.."/C net use t: \\\\SERVER\\multimedia /user:indiana jones"
so net.exe just needs "use t: \\\\server\\multimedia /user:indiana jones" .. without the "/C net" ???

been at it for an hour now.. I just keep getting this..
Index (zero based) must be greater than or equal to zero and less than the size of the argument list.

code looks like this now..
<pre>
System.Diagnostics.Process login;
login = new System.Diagnostics.Process();

login.EnableRaisingEvents = false;

string mapDriveP;
mapDriveP = "use t: \\\\server\\multimedia /user:" + textBox1.Text + " " + textBox2.Text;
string mapDriveS;
mapDriveS = "use s: \\\\server\\temp /user:" + textBox1.Text + " " + textBox2.Text;
string mapDriveU;
mapDriveU = "use u: \\\\server\\user /user:" + textBox1.Text + " " + textBox2.Text;

string cmdLine = string.Format("{0} {1} {2}", mapDriveP + mapDriveU + mapDriveS);
System.Diagnostics.Process.Start("NET.EXE", cmdLine);
</pre>

I feel like I am so close... Please advise...
Sergey Alexandrovich Kryukov 2-May-11 0:14am    
Please see updated Solution
--SA
Sergey Alexandrovich Kryukov 2-May-11 0:06am    
Wait a second... Please tell me, can you get what you want when you manually run CMD.exe and then 1) run NET.EXE one-by-one; 2) in one command.

Frankly, I did not analyze your NET.EXT command line arguments, I only pointed out how to pass them. It seemed to me you already knew how to use NET.EXE, only had a problem to run them through Process.Run...
--SA
Hooki 2-May-11 2:25am    
I'm still having a hard time.... I'm fairly new to C# two weeks, and I tend not to bother you with code... but this was the task..


I had a batch file..
net use u: \\server\multimedia /user:indiana jones
net use p: \\server\temp /user:indiana jones
net use s: \\server\user /user:indiana jones

and if I run that batch file.. It all executes in one System window..
Then I made an exe. (see code) and it runs in 3 almost simultaneously openend windows.. My initial code works

System.Diagnostics.Process.Start("CMD.exe", mapDriveP);
System.Threading.Thread.Sleep(2000);
System.Diagnostics.Process.Start("CMD.exe", mapDriveS);
System.Threading.Thread.Sleep(2000);
System.Diagnostics.Process.Start("CMD.exe", mapDriveU);
login.Close();

That works but I have to stop the Thread for 4 seconds to get the desired result. I even used steamwriter options try to
realize it..

The only thing the code has to do is map these 3 drives. Which it does but the net use command needs to be executed in 1 console window just like the .bat file> I'm stuck here. as the network is slow.. I have to pause the thread for 10 seconds each.. :-(

Would you be so kind to

put one of the map drive batch samples in
string[] parameters = new string[] {"using...", "using...", "using...", } //put complete command line


because the "using...." it's not clear to me what I need to do..
I am trying to do a ping 8.8.8.8 and a ping 8.8.4.4 as practice..
but I can't even get that to work

Batch file looks like this
=====
ping 8.8.8.8
ping 8.8.4.4

I want this to be run in one console window... I can't seem to get it to work my script always opens two console windows..

Please advise and thank you for the help so far
Sergey Alexandrovich Kryukov 2-May-11 3:46am    
Read my lips: "Do not sleep!!!!". Don not use "CMD.EXE"!!!!!!!!!!!!!
Use "process.WaitForExit()" -- only!!!!!!!! Use "NET.EXE" instead of "CMD.EXE".

Batch file is another option. You can use it instead of "CMD.EXE".

"Do not sleep!!!!". Don not use "CMD.EXE"!!!!!!!!!!!!!
--SA
Thanks for the reply dforbes
but I was thinking more along a solution like ...

System.Diagnostics.Process.Start("CMD.exe", mapDriveP + mapDriveU + mapDriveS);


Something like that.. that only 1 system window opens and all the drive map strings are
executed.. the original batch file has 3 net use lines ..

========
net use u: \\server\multimedia /user:indiana jones
net use p: \\server\temp /user:indiana jones
net use s: \\server\user /user:indiana jones

=======

the problem with this is if I give every user his own file they might "edit"
the bat files and see eachothers password.
I even thaugt of making an exe for each user :-)
 
Share this answer
 
Comments
yesotaso 1-May-11 13:17pm    
There is "&" operator I remember from somehere, which enables lining up commands like "dir & cd .. & dir" but as far as I know it works like logical "and" operator if a command in a row fails rest wont execute. Or maybe it was so in unix...
Sergey Alexandrovich Kryukov 1-May-11 17:33pm    
Not "CMD.EXE" but "NET.EXE"! "CMD.EXE" is not needed at all. Also, avoid multiple "+" with strings.
I vote 4 because the answer looks in principle correct.

Please see my answer.
--SA
Just curious (you know what happened to the cat)...

Why not use NET USE [DRIVE LETTER] [UNC PATH].....?
You can use %1, %2, %3... for variables.
Drag out an old DOS 5.0 manual. It has a great section on how it works. You can change the extension to '.cmd' for newer versions of Windows (not that it matters). You can pass the variables directly to the command window.

Hope this helps. Don't re-invent the wheel. DOS is old, but very stable, and you get expected results.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-May-11 17:39pm    
What DOS??! There is not such thing. There is full help on NET.EXE.
--SA
and the final Working Code..

C#
string credentials;
credentials = textBox1.Text + " " + textBox2.Text;


string[] parameters = new string[]
        {
        @"use * /delete /y",
        @"use p: \\cpserver\cpdata /user:" + credentials,
        @"use u: \\cpserver\user /user:" + credentials,
        };

foreach (string cmdLineParameter in parameters)
{
    System.Diagnostics.Process process =
       System.Diagnostics.Process.Start("NET.EXE", cmdLineParameter);
    process.WaitForExit();
}
MessageBox.Show("Login Completed");


This works Fine.. I thank you all for the effort and patience..
especially SAKryukov..

If there is any optimization to my code.. Let me know
 
Share this answer
 
While help does exist compiled into Net.exe, it does not have information about batch (.cmd) files.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900