当用户单击按钮时,我希望它运行登录脚本(从服务器启动),但是每台计算机都位于不同的服务器中,因此我获得了服务器名称。但是netlogon.StartInfo.Arguments = slnres + @"/c \netlogon\logon.cmd";
行无法正常工作。它应在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();
}
答案 0 :(得分:1)
几件事:
您无需运行命令提示符即可获取环境变量。您可以使用Environment.GetEnvironmentVariable
。
您对Arguments
的调用的logon.cmd
属性正在构建为:
\\myserver/c \netlogon\logon.cmd
当我认为您想要这样做时:
/c \\myserver\netlogon\logon.cmd
因此,请确保将slnres
放在字符串的正确位置。您的代码应如下所示:
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();
}
答案 1 :(得分:0)
我对您的问题有点困惑,我不确定我是否正确理解您。前一段时间,我制作了一个程序,其中必须运行一些powershell命令,所以我为此做了一个类。重定向到您的按钮,看起来像这样:
(请记住,您需要在文件所在位置输入fqdn => 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;
}
}