Solved

Shell command execute

Posted on 2004-09-09
9
2,261 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
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!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

724 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