问题描述
我已经知道使用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!
推荐答案
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命令的所有方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!