在C#中执行DOS命令的所有方法? [英] All ways to execute a dos command in C#?

查看:52
本文介绍了在C#中执行DOS命令的所有方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经知道使用System.Diagnostic.Process来执行带有参数(dos cmd)的cmd.exe,但这就是全部吗?我想知道是否有其他方法可以在Csharp中执行dos cmd,如果可能的话在VB.Net中也可以. (我记得我们可以在Vb6.0中使用Shell命令,并且想知道Vb.Net是否具有升级版本吗?).
我也知道在Sql Server中使用扩展过程来执行Dos cmd,所以请让我知道我不知道的新方法.

非常感谢您!

I have already known about using System.Diagnostic.Process to execute cmd.exe with arguments (dos cmd), but is that all? I would like to know if there is any other way to execute a dos cmd in Csharp and if possible in VB.Net as well. (I remember that we could use Shell command in Vb6.0 and wonder if Vb.Net has an upgraded version?).
I have also known about using an extended procedure in Sql Server to execute Dos cmd, so please let me know only new ways that I don''t know.

Thank you very much!

推荐答案

VB.Net与VB一样具有shell函数,但是为什么您要满足如此有限的调用呢?使用进程,您几乎可以拥有所有可能性,例如获取stdout,stderr,写入stdin,定义特定于进程的参数等等.

在sql server中,您有xp_cmdshell,但是如果需要定期执行OS命令并且预定义了命令,则可以将C#DLL嵌入数据库中,并在该程序集中使用Process类来完成OS特定部分.
VB.Net has a shell function just like VB did, but why would you settle for so limited call? With process you have (almost) all possibilities, like getting stdout, stderr, writing to stdin, defining process specific parameters and so on.

In sql server you have xp_cmdshell, but if the need to execute OS commands is regular and the commands are predefined I would embed a C# DLL inside the database and use Process class in that assembly to do the OS specific portion.


首先,在任何名为"Windows"的 OS 中都没有诸如"DOS命令"之类的东西.当Window是基于DOS的图形环境时,它可用.无论您现在运行什么,都是正常的Win32或64位Windows应用程序,处于成熟状态,处于保护模式等.因此,无法运行DOS命令". Shell命令不是DOS命令,甚至不能关闭.

要以Shell的方式启动任何过程,只需使用System.Diagnostics.Process.Start,请参阅 http ://msdn.microsoft.com/zh-CN/library/system.diagnostics.process.aspx [
First of all, there is no such thing as "DOS Command" in any OS named "Windows". It was available when Window was a graphical environment over DOS. Whatever you run now, is a regular Win32 or 64-bit Windows application, fully-fledged, in protected mode, etc. So, there is no way to "run a DOS command". Shell command is not a DOS command, not even close.

To start any process the way Shell does, simply use System.Diagnostics.Process.Start, see http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx[^].

—SA


您可以使用WMI.这是一个例子.

You can use WMI. Here is one example.

 public bool CreateMailBox( string MachineName, string UserName, string Password, string domain, string domainController,string identity, string alias, string database)
         {
             string db = database.Replace(" ", "::");
             

             string DriveLetter = ConfigurationManager.AppSettings["EXEDriveLetter"];
             ManagementPath myPath = new ManagementPath();
             myPath.NamespacePath = @"root\CIMV2";

             ConnectionOptions oConn = new ConnectionOptions();
             oConn.Username = UserName;
             oConn.Password = Password;
             oConn.EnablePrivileges = true;
             myPath.Server = MachineName;

             ManagementScope scope = new ManagementScope(myPath, oConn);
             scope.Connect();
             ManagementPath path2 = new ManagementPath();
             path2.Server = MachineName;
             path2.ClassName = "Win32_Process";
             path2.NamespacePath = @"root\CIMV2";
             ManagementScope scopeProcess = new ManagementScope(path2, oConn);

             using (ManagementClass process = new ManagementClass(scopeProcess, path2, null))
             {
                 // string commandLine = @"cmd /C C:\setperm.bat " + TempName +" "+"0909020";
                 string commandLine = @"cmd /C CreateMailBox.exe " + domain +" "+domainController+" "+identity+" "+alias+" "+db+" "+UserName+" "+Password;

                 using (ManagementBaseObject inParams = process.GetMethodParameters("Create"))
                 {
                     inParams["CommandLine"] = commandLine;
                     inParams["CurrentDirectory"] = DriveLetter + @":\\";
                     inParams["ProcessStartupInformation"] = null;
                     using (ManagementBaseObject outParams = process.InvokeMethod("Create", inParams, null))
                     {
                         int retVal = Convert.ToInt32(outParams.Properties["ReturnValue"].Value);

                         return (retVal == 0);
                     }
                 }
             }
                                    
            
         }


notice the commandLine . Here you pass your own command line command to execute. 


这篇关于在C#中执行DOS命令的所有方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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