如何在 for 循环的命令末尾添加递增数字?
How do you add incrementing numbers to the end of a command for a for loop?
由于进程不同,我想为每个进程组添加不同的编号。我认为这种语法可以使 [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]);
}
我认为你正在寻找一个数组;像这样:
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>
,但我更喜欢数组,因为集合将具有预定义的边界 (strings.Count()
).
你也可以这样做来达到同样的效果,
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]);
}
这就是我认为您可以用于基于索引的名称。
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);
}
现在您可以通过 p("newProcessInfo1")
、p("newProcessInfo2") 等或 p("newProcessInfo" + i.ToString())
等方式访问您的 processInfo,其中 i
具有整数值。
由于进程不同,我想为每个进程组添加不同的编号。我认为这种语法可以使 [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]);
}
我认为你正在寻找一个数组;像这样:
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>
,但我更喜欢数组,因为集合将具有预定义的边界 (strings.Count()
).
你也可以这样做来达到同样的效果,
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]);
}
这就是我认为您可以用于基于索引的名称。
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);
}
现在您可以通过 p("newProcessInfo1")
、p("newProcessInfo2") 等或 p("newProcessInfo" + i.ToString())
等方式访问您的 processInfo,其中 i
具有整数值。