由于流程不同,我想为每个流程组添加不同的编号。我认为这种语法可以使[i]的每个循环成为一组newProcessInfo1,newProcessInfo2,newProcessInfo3,....
string output = string.Empty;
for (int i = 0; i < strings.Count(); i++)
{
var newProcessInfo[i] = new System.Diagnostics.ProcessStartInfo();
newProcessInfo[i].FileName = @"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe";
newProcessInfo[i].WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
newProcessInfo[i].Verb = "runas";
newProcessInfo[i].Arguments += @"-executionpolicy unrestricted -Command " + strings[i];
System.Diagnostics.Process.Start(newProcessInfo[i]);
}
答案 0 :(得分:0)
我认为你正在寻找一个数组;像这样的东西:
System.Diagnostics.ProcessStartInfo[] arrayProcessInfo= new System.Diagnostics.ProcessStartInfo[strings.Count()];
这样您就可以像下面这样添加它们:
for (int i = 0; i < strings.Count(); i++)
{
arrayProcessInfo[i] = new System.Diagnostics.ProcessStartInfo();
arrayProcessInfo[i].FileName = @"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe";
arrayProcessInfo[i].WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
arrayProcessInfo[i].Verb = "runas";
arrayProcessInfo[i].Arguments += @"-executionpolicy unrestricted -Command " + strings[i];
System.Diagnostics.Process.Start(arrayProcessInfo[i]);
}
它最初会创建一个ProcessStartInfo
数组,包含strings.Count()
个索引。然后通过循环,您可以为每个数组索引创建新的ProcessStartInfo;
或者您可以使用List<ProcessStartInfo>
,但我更喜欢Array,因为Collection将具有预定义边界(strings.Count()
)。
答案 1 :(得分:0)
你也可以做同样的事情来实现同样的目标,
Dictionary<int, System.Diagnostics.ProcessStartInfo> incrementalArray = new Dictionary<int, System.Diagnostics.ProcessStartInfo>(); ;
for (int i = 1; i < 5; i++)
{
incrementalArray.Add(i, null);
}
然后从列表中取值进行更改
string output = string.Empty;
for (int i = 0; i < strings.Count(); i++)
{
incrementalArray[i] = new System.Diagnostics.ProcessStartInfo();
incrementalArray[i].FileName = @"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe";
incrementalArray[i].WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
incrementalArray[i].Verb = "runas";
incrementalArray[i].Arguments += @"-executionpolicy unrestricted -Command " + strings[i];
System.Diagnostics.Process.Start(incrementalArray[i]);
}
答案 2 :(得分:0)
我认为这可以用于基于索引的名称。
Dictionary<string, System.Diagnostics.ProcessStartInfo> p = new Dictionary<string, System.Diagnostics.ProcessStartInfo>(); ;
string pName = "newProcessInfo";
for (int i = 1; i <= strings.Count(); i++)
{
string indexedProcess = pName + i.ToString();
p.Add(indexedProcess, new System.Diagnostics.ProcessStartInfo());
System.Diagnostics.ProcessStartInfo pInfo = p(indexedProcess);
pInfo.FileName = @"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe";
pInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
pInfo.Verb = "runas";
pInfo.Arguments += @"-executionpolicy unrestricted -Command " + strings[i];
System.Diagnostics.Process.Start(pInfo);
}
现在您可以访问您的processInfo为p("newProcessInfo1")
,p(“newProcessInfo2”)等,或者p("newProcessInfo" + i.ToString())
,其中i
具有整数值。