Reputation: 2010
I want to use CMD command in c# winforms to create a folder to a DIR, with a given directory path,
e.g.
"C:\temp> MD MyFolder"
I wonder how can i execute it using c# win forms platform. I tried looking at google but couldn't find anything, and is it a good practise to use it, the only reason i wanna use it beacause my directory path is too long for Directory.CreateDirectory()
method.
I worked out that CMD have the same limitations as "CreateDirectory" method in C#. Thanks for your comment and answers people.
Upvotes: 1
Views: 3237
Reputation: 610
To make command line process use it:
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true
}
};
Now for data handling use this:
// For output data directly from command line.
process.OutputDataReceived += outputReceivedHandler;
process.BeginOutputReadLine();
// For error output directly from command line.
process.ErrorDataReceived += errorReceivedHandler;
process.BeginErrorReadLine();
// For providing an command to command line.
var command = "ping www.google.pl";
var writer = process.StandardInput;
writer.WriteLine(command);
process.WaitForExit();
process.Close();
Finally method that listening for output:
public void outputReceivedHandler(object sender, DataReceivedEventArgs e)
{
if (e.Data != null || !string.IsNullOrEmpty(e.Data))
Console.WriteLine(e.Data);
}
Of course this same for error handling. Hope this can help anybody. I searching that a lot and I don't found the answer, only some directions for make it, now I can help another people who has this same problem. Sorry if my language is not so good.
Upvotes: 1
Reputation: 62169
All these answers about Process.Start
are wonderful, but I am afraid that we are before yet one more case of an OP asking us to help them implement the solution which they think is going to do what they are trying to achieve instead of asking us how to achieve what they are really trying to achieve.
Luckily, the OP has included enough information so that we can guess what they are trying to achieve and give them an answer which might actually be useful to them.
So:
My very first recommendation would be that you fix your directory path so that it is not too long for the Directory.CreateDirectory()
method, because that's insane, and it is bound to sooner or later cause you problems that you are going to bitterly regret for.
If you insist on creating your directory someplace close to the root, use the very fine System.IO.Path.Combine()
method, to build the full path to your directory, so that you can call Directory.CreateDirectory()
, instead of trying to create it from within the current directory in which your application is running, or coming up with bizarre ideas about launching a command processor to create your directory for you.
Also, please forget about DOS. These commands have been called console commands
for the last 18 years, ever since the release of Windows NT 3.1 back in 1993.
Upvotes: 3
Reputation: 31251
This will run the command, the cmd window will be hidden as well, I have used this a lot and it works flawlessly.
ProcessStartInfo cmd = new ProcessStartInfo("cmd.exe");
cmd.RedirectStandardInput = true;
cmd.RedirectStandardOutput = true;
cmd.RedirectStandardError = true;
cmd.UseShellExecute = false;
cmd.CreateNoWindow = true;
cmd.WindowStyle = ProcessWindowStyle.Hidden;
Process console = Process.Start(cmd);
console.StandardInput.WriteLine("md MyFolder");
Hope this helps!
Upvotes: 0
Reputation: 499392
You can use Process.Start
to execute an external executable. In the case of built in commands you need to make sure that you start it with the cmd.exe
executable (passing in the parameters with the /c
flag).
Note that an OS limit can't be overcome with this method either.
Upvotes: 0
Reputation: 40756
To overcome the 256 characters limit, I once wrote a tiny library for .NET.
Basically this library contains several P/Invoke wrappers to underlying Win32 functions that automatically prepend the \\?\
suffix to indicate a path longer than MAX_PATH
.
Upvotes: 2
Reputation: 2342
System.Diagnostics.Process.Start("cmd.exe", "/c MD C:\\YourFolder");
btw. I really do not get what you mean by "Directory.CreateDirectory does not work".
Upvotes: 0