Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

DOS commands in C#

Posted on 2003-03-22
4
Medium Priority
?
341 Views
Last Modified: 2012-08-14
Hello everyone,

Can someone pls explain how to run typical DOS commands in C# code? For example open ftp connection to some site, provide username and password, get some file and save it in, say, C:\project. Or rename, delete, move a file, just the basic things. Please be as detailed as possible...

Thanks, Nevez
0
Comment
Question by:nevez
  • 2
  • 2
4 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 8189183
To run any process (including DOS commands) from C# code you need to use Process.Start method:

System.Diagnostics.Process p = new System.Diagnostics.Process();
p.Start("del", "C:\MyFile.txt");  // delete file using DOS del command

To have more control over process start operation you can use other overloaded function:

public static Process Start(
   ProcessStartInfo startInfo
);

To wait for the end of process use Process.WaitForExit Method or handle Exited event. When process finished use Process.ExitCode Property to get exit code.
0
 

Author Comment

by:nevez
ID: 8190199
The above example

p.Start("del", "C:\MyFile.txt");

doesn't really work. Error message is: "Static member 'System.Diagnostics.Process.Start(string, string)' cannot be accessed with an instance reference; qualify it with a type name instead"

But anyway, can you pls. give an eample specifically with an ftp connection?

Thnaks
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 400 total points
ID: 8193387
Sorry, it was my mistake. Static function should be called by other way:

Process p = Process.Start(del", "C:\MyFile.txt");
p.WaitForExit();   // wait for process end

Replace program name and arguments in this sample with any other values that you need.
ftp command works in interactive mode, you need to use the function
Start(ProcessStartInfo startInfo)
and redefine stdin and stdout for created process, and supply answers for ftp command request. This is complicated problem, I think using .NET networking classes is the best way.
0
 

Author Comment

by:nevez
ID: 8196058
I'm beginning to realize the complexity of this. Thanks for your input.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question