Solved

Shell command execute

Posted on 2004-09-09
9
2,239 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 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

15 Experts available now in Live!

Get 1:1 Help Now