2016-07-01 9 views
2

Wegen der verschiedenen Prozesse möchte ich für jede Prozessgruppe eine andere Nummer hinzufügen. Ich dachte, diese Syntax jede Schleife haben funktionieren würde [i] zum Beispiel einer Gruppe von newProcessInfo1, newProcessInfo2, newProcessInfo3, zu sein ....Wie fügen Sie am Ende eines Befehls inkrementierende Zahlen für eine for-Schleife hinzu?

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

} 
+1

Was Sie genau machen wollen, bitte erläutern Sie ausführlich und auch, was Sie bisher versucht haben? –

+1

Wie Sie wissen, wird 'for loop' auf der Grundlage Ihrer' strings.Count() 'mit' i ++ 'selbst inkrementiert. –

+1

http://StackOverflow.com/a/32222247/2613020 kann Ihnen helfen –

Antwort

0

Ich glaube, Sie für einen Array suchen; so etwas wie dieses:

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

Es wird zunächst Erstellen Sie ein Array von ProcessStartInfo enthält strings.Count() Indizes:

System.Diagnostics.ProcessStartInfo[] arrayProcessInfo= new System.Diagnostics.ProcessStartInfo[strings.Count()]; 

so dass Sie sie wie folgt hinzufügen können. Und später durch Schleifen können Sie neue ProcessStartInfo für jeden Array-Index erstellen;

Alternativ können Sie List<ProcessStartInfo> verwenden, aber ich bevorzuge Array, da die Sammlung Predefined Boundries (strings.Count()) haben wird.

0

Sie auch so etwas tun kann, das gleiche zu erreichen,

Dictionary<int, System.Diagnostics.ProcessStartInfo> incrementalArray = new Dictionary<int, System.Diagnostics.ProcessStartInfo>(); ; 
for (int i = 1; i < 5; i++) 
{ 
    incrementalArray.Add(i, null); 
} 

Dann nehmen Sie Werte aus der Liste, um die Änderung zu machen

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

Dies ist, was ich glaube, Sie verwenden können für indizierte Namen.

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

Jetzt können Sie Zugriff auf Ihre Process als p("newProcessInfo1"), p ("newProcessInfo2") usw. oder als p("newProcessInfo" + i.ToString()), wo i Integer-Wert hat.

+0

Genau das habe ich gesucht! –

Verwandte Themen