J'ai un programme qui accepte une commande Cmd en tant qu'argument de commande.Transmission de la commande Cmd à C# Application
Fondamentalement, vous appelez cette façon: C:\MyProgram.exe del C:\test.txt
La commande ci-dessus fonctionne très bien. Cependant, lorsque je tente de faire une commande xcopy échoue:
C:\MyProgram.exe xcopy C:\test.txt C:\Temp\Test2.txt
Le code du programme:
class Program
{
static void Main(string[] args)
{
string command = GetCommandLineArugments(args);
// /c tells cmd that we want it to execute the command that follows and then exit.
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", @"/D /c " + command);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo = procStartInfo;
process.Start();
}
private static string GetCommandLineArugments(string[] args)
{
string retVal = string.Empty;
foreach (string arg in args)
retVal += " " + arg;
return retVal;
}
}
Est-ce que C: \ test.txt existe lorsque vous exécutez la commande xcopy? –
Comment cela échoue-t-il? Quelle erreur cela donne-t-il? – David
@Jimmy Oui, le répertoire ci-dessus était un répertoire, mais dans notre vrai appel du programme le répertoire existe. – mint