Solved

Shell command execute

Posted on 2004-09-09
9
2,256 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Header Font Size in Grid View 6 45
ComboBox to String Not Working 5 42
aspx ascx, c# 7 40
async questions 5 48
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 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