Click here to Skip to main content
15,885,899 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

C#
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.
Kirangowle
Posted
Comments
[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.
--SA
[no name] 21-Sep-12 11:33am    
There is no need at all.

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:
C#
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.

—SA
 
Share this answer
 
v2
Comments
fjdiewornncalwe 21-Sep-12 11:36am    
+5. Well explained.
Sergey Alexandrovich Kryukov 21-Sep-12 11:42am    
Thank you, Marcus.
--SA
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.
--SA
Christiaan Rakowski 21-Sep-12 17:16pm    
Easy solutions are typically the best ones, +5 from me.
You can see it at: How to Execute a Command in C# ?[^]. Hope this helps.
 
Share this answer
 
Comments
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