简体   繁体   中英

Command won't run in Command Prompt

When users click on a button, I want it to run the logon script(launching from server), but each computer in different servers, so I get the server name. But the netlogon.StartInfo.Arguments = slnres + @"/c \\netlogon\\logon.cmd"; line is not working as it should be. It should run the logon.cmd on the PC(mapping network drivers, printers, etc), and then the CMD should close.

 private void MapNetwork_Click(object sender, EventArgs e)
    {
        Process sln = new Process();
        sln.StartInfo.UseShellExecute = false;
        sln.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        sln.StartInfo.FileName = "cmd.exe";
        sln.StartInfo.Arguments = "/c echo %logonserver%";
        sln.StartInfo.RedirectStandardOutput = true;
        sln.Start();
        string slnres = sln.StandardOutput.ReadToEnd();
        label1.Text = slnres;

        Process netlogon = new Process();
        netlogon.StartInfo.UseShellExecute = false;
        netlogon.StartInfo.FileName = "cmd.exe";
        netlogon.StartInfo.Arguments = slnres + @"/c \netlogon\logon.cmd";
        netlogon.Start();
    }

A couple things:

  1. You don't need to run a command prompt to get an environment variable. You can use Environment.GetEnvironmentVariable .

  2. Your Arguments property for your call to logon.cmd is being constructed into this:

\\myserver/c \netlogon\logon.cmd

When I think you want this:

/c \\myserver\netlogon\logon.cmd

So make sure you put slnres at the right place in your string. Your code should look like this:

private void MapNetwork_Click(object sender, EventArgs e)
{
    string slnres = Environment.GetEnvironmentVariable("logonserver");
    label1.Text = slnres;

    Process netlogon = new Process();
    netlogon.StartInfo.UseShellExecute = false;
    netlogon.StartInfo.FileName = "cmd.exe";
    netlogon.StartInfo.Arguments = "/c " + slnres + @"\netlogon\logon.cmd";
    netlogon.Start();
}

i am a little confused about your question and i am not rly sure if i understand you correctly. some time ago i made a program where i had to run few powershell commands, so i made a class for it. redirected to your button it would look like that:

(and remember you need the fqdn to your file location => Reading File From Network Location )

using System.Diagnostics;

    //class lvl scope vars
    string output;
    string ErrorOutput;

    private void MapNetwork_Click(object sender, EventArgs e)
    {
        //define process arguments
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = @"cmd.exe";
        startInfo.Arguments = @"FQDN path to your file on the server; exit";
        startInfo.UseShellExecute = false;
        startInfo.CreateNoWindow = true;
        startInfo.RedirectStandardOutput = true;
        startInfo.RedirectStandardError = true;

        //start process
        Process process = new Process();
        process.StartInfo = startInfo;
        process.Start();
        process.WaitForExit();

        //outpunt handling
        if (string.IsNullOrEmpty(ErrorOutput))
        {
            return output;
        }
        else
        {
            return ErrorOutput;
        }
    }
  1. first of all i would check if your application is able to open the file one the shared network location. (server available? access rights to server? serer mapped?)
  2. after that you can check if he is able to start the file locally. (does it need admin rights to run the *.cmd, *.bat file)
  3. now you can check if your application runs it correctly.

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