简体   繁体   中英

How to run command line code from within C# Windows Form?

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

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)

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().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM