从C#以编程方式运行“命令提示符" [英] Running 'command prompt' from C# programatically

查看:101
本文介绍了从C#以编程方式运行“命令提示符"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试从C#运行命令行提示符.

我尝试过的代码是这样;

 System.Diagnostics.Process进程=  System.Diagnostics.Process();
 System.Diagnostics.ProcessStartInfo stratInfo =  System.Diagnostics.ProcessStartInfo();
 stratInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
 stratInfo.FileName = " ;
 stratInfo.Arguments = " ;
 process.StartInfo = stratInfo;
 process.Start();


这是行不通的.有人有其他想法可以做到这一点,或者看到我的代码中的缺陷吗?

谢谢

解决方案

ProductReview_Accessory是为SERVICE_NAME或DISPLAY_NAME供电吗?
sc命令需要SERVICE_NAME.您可以使用sc query来找到您所使用的服务的SERVICE_NAME.

还有另一件事"sc delete"给出了此提示:

从注册表中删除服务条目.
如果服务正在运行,或者另一个进程有一个
打开服务句柄,将服务简单标记为
删除.



因此,您可能要先尝试执行 sc停止service_name ,然后再尝试将其删除.当然,如果仍然有开放的服务句柄,您就无法工作.
您可能还需要具有适当的访问权限.


干杯,


曼弗雷德(Manfred)


什么不起作用?我敢打赌,该文件夹或其内容不会被删除,因为您尚未为该文件夹/文件指定完全限定的路径.

同样,以这种方式删除内容也是完全不必要的.您可以使用System.Io下的File/FileInfo或Directory/DirectoryInfo类来删除文件/文件夹.因此,稍后我通过以下参考资料找到了解决方案:
如何在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

解决方案

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


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.


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# ?[^]


这篇关于从C#以编程方式运行“命令提示符"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆