[英]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";
但是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. 它应该在PC(映射网络驱动程序,打印机等)上运行logon.cmd,然后CMD应该关闭。
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: 几件事:
You don't need to run a command prompt to get an environment variable. 您无需运行命令提示符即可获取环境变量。 You can use Environment.GetEnvironmentVariable
. 您可以使用Environment.GetEnvironmentVariable
。
Your Arguments
property for your call to logon.cmd
is being constructed into this: 您对logon.cmd
的调用的Arguments
属性正在构造为:
\\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. 因此,请确保将slnres
放在slnres
中的正确位置。 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. 前一段时间,我制作了一个程序,其中必须运行一些powershell命令,所以我为此做了一个类。 redirected to your button it would look like that: 重定向到您的按钮,它看起来像这样:
(and remember you need the fqdn to your file location => Reading File From Network Location ) (并记住,您需要将fqdn放在您的文件位置=> 从网络位置读取文件 )
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.