Click here to Skip to main content
15,910,234 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello I am trying to run a command line prompt from C#.

The code that I tried for it is this;
C#
System.Diagnostics.Process process = new System.Diagnostics.Process();
 System.Diagnostics.ProcessStartInfo stratInfo = new System.Diagnostics.ProcessStartInfo();
 stratInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
 stratInfo.FileName = "cmd.exe";
 stratInfo.Arguments = "/C sc delete \"ProductReview_AccessoryPower\"";
 process.StartInfo = stratInfo;
 process.Start();

This is not working. Someone have another idea to accomplish this or see the flaw in my code?

Thanks
Posted
Updated 14-Dec-10 10:54am
v4
Comments
johannesnestler 14-Dec-10 17:39pm    
what is not working?
if i look at your code:
calling and executing command-line - is ok
command for deletion? are you sure you can call it like this? full path?

Is ProductReview_AccessoryPower the SERVICE_NAME or the DISPLAY_NAME?
The sc command needs the SERVICE_NAME. You can use sc query to find out what the SERVICE_NAME for your service is.

One more thing "sc delete" gives this hint:
Deletes a service entry from the registry.
If the service is running, or another process has an
open handle to the service, the service is simply marked
for deletion.


So you might want to do a sc stop service_name first before you try to delete it. Wont work of course when there are still open handles to your service.
You'll probably also need to have the proper access rights.


Cheers,


Manfred
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 9-Jan-11 19:28pm    
This is correct - my 5, another answer is irrelevant.
WHAT isn't working?? I'd be will to bet the folder or its contents are not being deleted because you haven't specified a fully qualified path to the folder/files in question.

Also, deleting stuff in this manner is completely unneccessary. You can use the File/FileInfo or Directory/DirectoryInfo classes under System.Io to delete files/folders.
 
Share this answer
 
Comments
Orcun Iyigun 14-Dec-10 17:03pm    
I am trying to delete a service not a file nor folder. The command there is for deletion of a service.
I tried to stop the service before deleting it but didnt work out well. So later on I found the solution to it by this reference:
How to Execute a Command in C# ?[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900