?
Solved

Shell command execute

Posted on 2004-09-09
9
Medium Priority
?
2,266 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 1000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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 is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

770 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