1

I am fairly new to coding, but have built a few small things. One thing I figured out on my last project was how to run 2 simple commands normally run from a console, but from within a form application instead. Simply, the form had 2 buttons and clicking one caused ipconfig to run and the other ipconfig /all. It then posted the ip information coming from the command into another form I created as a message box. That is important because I am trying to do something similar and nothing is working now.

I have a form that has a spot for user name and a spot for password. On submit, I want it to essentially run the following:

NET USE F: \\ALPHA\CLIENTAPPS /user:domain\%username% %password% /persistent:no
NET USE O: \\ALPHA\USERS /user:domain\%username% %password% /persistent:no
NET USE S: \\ALPHA\COMPANY /user:domain\%username% %password% /persistent:no

Where %username% and %password% are captured from the form and domain will be our actual domain.

Using similar methods to the aforementioned ipconfig program that is working, this is what I came up with. However, when I click the Submit button, nothing happens, no errors, nor does it actually create the network share:

private void btnSubmit_Click(object sender, EventArgs e)
{
    string un = txtUsername.Text;
    string pw = txtPassword.Text;

    System.Diagnostics.ProcessStartInfo PR = new System.Diagnostics.ProcessStartInfo("cmd", @" /c net use W: \\\\ALPHA\\CLIENTAPPS /user:acsdish\\" + un + " " + pw + "/persistent:no");
    PR.RedirectStandardOutput = true;
    PR.UseShellExecute = false;
    PR.CreateNoWindow = true;
    System.Diagnostics.Process StartPR = new System.Diagnostics.Process();
    StartPR.StartInfo = PR;
    StartPR.Start();
}

What am I missing here, or is there a better way? Thanks.

Mike

Adi Lester
  • 24,731
  • 12
  • 95
  • 110
shelzmike
  • 173
  • 3
  • 13
  • This might help: http://stackoverflow.com/questions/4807097/start-a-command-line-including-arguments-from-c-sharp – Almo Aug 09 '12 at 21:41

2 Answers2

5
System.Diagnostics.ProcessStartInfo PR = new System.Diagnostics.ProcessStartInfo("cmd", @" /c net use W: \\\\ALPHA\\CLIENTAPPS /user:acsdish\\" + un + " " + pw + "/persistent:no");

Try to remove "@" or remove escaping of "\" char

Info here (Verbatim string literals)

dantix
  • 745
  • 1
  • 5
  • 14
  • Great info! It is always the little things that offer the most benefit. So, basically, if I use the @ I do not have to escape the \ characters (or anything really)? Thanks again! – shelzmike Aug 10 '12 at 14:06
3

nothing happens, no errors, nor does it actually create the network share

You've done a lot to ensure that. "No errors" is easy to explain, you don't check for errors nor do you give a way for the user to see them because you made sure that the console window isn't visible. If the command failed that it won't be visible. Checking Process.ExitCode is a minimal requirement.

Next flaw is that you create the mapping to the share for a particular user. Which is fine, drive mappings are a per-user setting. But you are not actually logged-in as that user so you can't see those mappings. You'll have to hit Ctrl+Alt+Del and switch the user account. But that's a lost cause because you passed /persistent:no. That means "persistent while the user is logged in".

Ultimate flaw is that you leave it up to an another process to take care of it. That always loses critical information, especially errors. You should pinvoke the Windows api function that does this so you know when it doesn't work and don't burn a gazillion cycles to run another process. Pinvoke WNetAddConnection2().

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Your information is helpful, so thanks for that. Actually, in between me posting and actually checking back in here, I came across the idea somewhere else that using the API would be a better option. I did find this [link] http://www.codeproject.com/Articles/6847/Map-Network-Drive-API?msg=4334657#xx4334657xx which seems to be good. I was able to use the core and modify to fit my needs. However, This seems to be using the older WNetAddConnection though. I will check out 2 to see if that is more practical, or rather if I can figure it out easily. Thanks again. – shelzmike Aug 10 '12 at 14:04
  • I have more problems going on with trying to get the WNetAddConnection2() to work; however, it is not relevant to the specific question I asked initially so I will search for the answer and if I cannot find it, I will post another one that is related specifically to using that function. Thanks again! – shelzmike Aug 10 '12 at 18:06