如何在for循环的命令末尾添加递增的数字

本文关键字:添加 数字 命令 for 循环 | 更新日期: 2023-09-27 17:53:50

由于进程不同,我想为每个进程组添加不同的编号。我认为这种语法可以使[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]);
}

如何在for循环的命令末尾添加递增的数字

我认为你正在寻找一个数组;像这样:

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]);
}

它将首先创建一个包含strings.Count()索引的ProcessStartInfo数组。然后通过循环,你可以为每个数组索引创建新的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);
}

然后从List中获取值来进行更改

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);
}

现在你可以访问你的processInfo作为p("newProcessInfo1"), p("newProcessInfo2")等或p("newProcessInfo" + i.ToString()),其中i是整数值