Solved

Shell command execute

Posted on 2004-09-09
9
2,232 Views
Last Modified: 2012-06-22
Is there any possible way to execute shell command (like: "ren something.txt sth.txt" or "copy *.exe c:\tmp\") in c#. I'm searching for the solution for quite a time, but all i managed to find is the execution of *.exe files (Process.Start(.....))
0
Comment
Question by:ssdjgru1
9 Comments
 
LVL 20

Expert Comment

by:TheAvenger
ID: 12015705
You can use Process.Start to execute shell commands as well. Just write the command as an argument of the Process.Start method
0
 

Author Comment

by:ssdjgru1
ID: 12016099
I tried something like Process.Start("","del c:\\work\\sample.txt"); but it crashes with explanation: Cannot start process because a file name has not been provided.
if I try  Process.Start("del","c:\\work\\sample.txt"); the syytem returns: The system cannot find the file specified.
0
 
LVL 20

Accepted Solution

by:
TheAvenger earned 250 total points
ID: 12016306
OK, make it like this:

Process.Start ("cmd", "/K del c:\\work\\sample.txt");

This will execute the command and leave the command prompt open. If you want to also close it, replace /K with /C. For more info click Start->Run, type in cmd and press enter. In the command prompt write cmd /? and read the documentation there.
0
 
LVL 23

Expert Comment

by:rama_krishna580
ID: 12017157
Hi,

Look at here, its some example it may help you..
http://www.thecodeproject.com/csharp/csdoesshell2.asp

R.K
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 22

Expert Comment

by:cookre
ID: 12018769
ProcessStartInfo ProcInfo=new ProcessStartInfo();
try {
    ProcInfo.Arguments=" /C \""+VBSFileName+"\""; <----------------- name of file with known extension
    ProcInfo.CreateNoWindow=true; <-------------------------------- as you see fit
    ProcInfo.ErrorDialog=false;
    ProcInfo.ErrorDialogParentHandle=IntPtr.Zero;
    ProcInfo.FileName="c:\\windows\\system32\\CMD.exe";  <-------------- can vary among OSs
    ProcInfo.RedirectStandardError=true;
    ProcInfo.RedirectStandardInput=true;
    ProcInfo.RedirectStandardOutput=true;
    ProcInfo.UseShellExecute=false;
    ProcInfo.Verb="";
    ProcInfo.WindowStyle=ProcessWindowStyle.Hidden;
    Process.Start(ProcInfo);
    }
catch (Exception e)
      {
      .....
      }
0
 
LVL 15

Expert Comment

by:SRigney
ID: 12021333
Not to get off topic here, but if you are just looking to rename, move and delete files you probably could use the System.IO.Directory and System.IO.File objects to do most of that.

I would stay away from starting another process unless it's needed.

string workingFile = "c:\\work\\sample.txt";
if( System.IO.File.Exists(workingFile) )
   System.IO.File.Delete(workingFile);
0
 

Author Comment

by:ssdjgru1
ID: 12024000
Thanks everybody for anwsers!
I have another question on this topic...why this doesn't work and how to solve the problem? (running batch files from C#):
Process.Start("cmd","/K c:\\work\\exec.bat");
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 12024161
Have you tried directly starting the batch file:

Process.Start ("c:\\work\\exec.bat")
0
 

Author Comment

by:ssdjgru1
ID: 12024312
It works (:-)!
Thanks!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now