Click here to Skip to main content
15,302,965 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello All,

This is my first project/post in C# world.

I want to send and read the commands from DOS using C#.
For eg. creating/deleting a file using edit myfile.txt command

System.Diagnostics.Process proc = new System.Diagnostics.Process();
              proc.EnableRaisingEvents = false;
              string strCmdLine;
              strCmdLine = "edit kiran.txt";
              System.Diagnostics.Process.Start("CMD.exe", strCmdLine);

but in above code only command prompt is getting opened.i cant able to create a file.

Thanks in advance.
[no name] 21-Sep-12 11:22am
Why don't you use the built-in .NET libraries to create this text file?
kirangowle 21-Sep-12 11:28am
Thanks Aday,for quick reply.
This is my first project so i want get control over DOS.
[no name] 21-Sep-12 11:30am
There is no "DOS" to be control of. If you really insist that you want to do this, this way, then you would need to take a look at the Process.StandardInput property
Sergey Alexandrovich Kryukov 21-Sep-12 11:31am
Of course, but I don't see a need for this in this case -- OP uses command line, just incorrectly.
The problem is using unwanted "CMD.EXE". I answered, please see.
[no name] 21-Sep-12 11:33am
There is no need at all.
Sergey Alexandrovich Kryukov 21-Sep-12 11:33am
I updated the commend and my answer. It's about "CMD.EXE".
Sergey Alexandrovich Kryukov 21-Sep-12 11:24am
This is .NET. On all systems which can host .NET, there is no such thing as "DOS", period.
Sergey Alexandrovich Kryukov 21-Sep-12 11:30am
By the way, what's "edit" and why using it? Executing a separate process is always bad; the processes are well isolated. Wes is right.
Otto Grunf 22-Sep-12 2:52am
EDIT, the EdLin's successor.
Sergey Alexandrovich Kryukov 23-Sep-12 11:51am
Anyway, please see my solution.

First, please see my comment to the question: in the scope of it, there is no such thing as "DOS". (And that was a root cause of your mistake.)

Now, to fix your problem you just need to understand that you don't need "CMD.EXE". This is nothing but yet another program which is irrelevant to the problem you are trying to solve: starting an application.

So, all you need is this:
string app = "edit";
string strCmdLine = "kiran.txt";
System.Diagnostics.Process.Start(app, strCmdLine);

Now, I don't know what is "edit", but is this is some text editor, chances are, you don't really want to start it; it won't be able to collaborate with your application. It would be more reasonable to create a UI application (WPF or System.Windows.Forms) and embed a simple edit control like TextBox in your application.

fjdiewornncalwe 21-Sep-12 11:36am
+5. Well explained.
Sergey Alexandrovich Kryukov 21-Sep-12 11:42am
Thank you, Marcus.
Andrewpeter 21-Sep-12 11:48am
I vote 5 for you, it's great solution.
Sergey Alexandrovich Kryukov 21-Sep-12 12:11pm
Thank you, Andrew.
Christiaan Rakowski 21-Sep-12 17:16pm
Easy solutions are typically the best ones, +5 from me.
Sergey Alexandrovich Kryukov 21-Sep-12 17:58pm
Thank you, Christiaan.
You can see it at: How to Execute a Command in C# ?[^]. Hope this helps.
fjdiewornncalwe 21-Sep-12 11:37am
+5. Probably overkill for what the OP wants, but a very good find nonetheless.
Andrewpeter 21-Sep-12 11:45am
Thanks you, Marcus.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900