Passing Cmd Command to C# Application
Asked Answered
Z

2

2

I have a program that accepts a Cmd command as a command argument.

Basically you call it this way: C:\MyProgram.exe del C:\test.txt

The above command works fine. However when I try to do an xcopy command it fails:

C:\MyProgram.exe xcopy C:\test.txt C:\Temp\Test2.txt

The code of the program:

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;
    }
}
Zebulun answered 6/8, 2010 at 18:12 Comment(6)
Does C:\test.txt exist when you execute the xcopy command?Genovevagenre
How does it fail? What error does it give?Misology
@Jimmy Yes, the above directory was a made up one, but in our real call of the program the directory exists.Zebulun
@Misology It doesn't seem to give any error, it just doesn't do anything! The delete command will work, but not xcopy. Permissions issue?Zebulun
Out of curiosity, is this a fun/educational project, or something you actually plan to use?Cringe
@baultista I actually had to make the program for a coworker to pass command line command from his program. Probably wasn't necessary, but only took a few minutes to make.Zebulun
C
2

I think Jimmy Hoffa answer is right, to solve it you can cocatenate "start " at the beggining of the command.

xcopy C:\temp\test.html C:\temp\test2.html will bring you a window with a prompt when needed.

Cocklebur answered 6/8, 2010 at 18:27 Comment(2)
adding start did it. thank you so much! ps jimmy i upvoted you, and i'm out of votes jonathan, i'll upvote you when i can.Zebulun
Thanks! (I hate the 15 chars limit in comments!)Cocklebur
G
6

I think your application is working, so I tried the command and just got this prompt for input from stdin:

C:\>xcopy C:\temp\test.html C:\temp\test2.html
Does C:\temp\test2.html specify a file name
or directory name on the target
(F = file, D = directory)?

Perhaps it's bombing because you don't have the stdin tied in, and your app is just returning the return code from the execution.

Genovevagenre answered 6/8, 2010 at 18:22 Comment(0)
C
2

I think Jimmy Hoffa answer is right, to solve it you can cocatenate "start " at the beggining of the command.

xcopy C:\temp\test.html C:\temp\test2.html will bring you a window with a prompt when needed.

Cocklebur answered 6/8, 2010 at 18:27 Comment(2)
adding start did it. thank you so much! ps jimmy i upvoted you, and i'm out of votes jonathan, i'll upvote you when i can.Zebulun
Thanks! (I hate the 15 chars limit in comments!)Cocklebur

© 2022 - 2024 — McMap. All rights reserved.