[英]How to check PostgreSql is installed or not in windows machine using C#.net?
[英]How to execute a command in a remote machine and get the output using c#.net
我正在使用WMI类在远程计算机中执行命令并在文件中写入输出。 如果该过程完成,我保持睡眠状态5秒钟,而5秒钟可以从文件中获取输出。 但是,如果该过程需要更多的时间来完成,那么我就不会有任何投入。
有人可以建议我如何等待远程过程完成,或者可以通过任何其他机制从远程计算机获取输出。
以下是我的代码
ConnectionOptions connOptions = new ConnectionOptions();
ObjectGetOptions objectGetOptions = new ObjectGetOptions();
ManagementPath managementPath = new ManagementPath("Win32_Process");
connOptions.Impersonation = ImpersonationLevel.Impersonate;
connOptions.EnablePrivileges = true;
ManagementScope manScope = new ManagementScope(String.Format(@"\\{0}\ROOT\CIMV2", computerName), connOptions);
manScope.Connect();
// Method Options
InvokeMethodOptions methodOptions = new InvokeMethodOptions(null, System.TimeSpan.MaxValue);
ManagementClass processClass = new ManagementClass(manScope, managementPath, objectGetOptions);
ManagementBaseObject inParams = processClass.GetMethodParameters("Create");
// Console.WriteLine(command);
inParams["CommandLine"] = "cmd /c " + command + " > c:\\tmp_dr.dat";
ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams,methodOptions);
Thread.Sleep(5000);
// then reading the file.
StreamReader sr = new StreamReader("\\\\" + computerName + "\\c$\\tmp_dr.dat");
line = sr.ReadLine();
while (line != null)
{enter code here
result += line + "\n";
line = sr.ReadLine();
}
sr.Close();
下面是您可以等待远程进程完成任务的一种方法
uint ProcessId = (uint)outParams["processId"];
if (ProcessId != 0)
{
while (true)
{
try
{
Process.GetProcessById((int)ProcessId, {your_remoteMachineName});
Thread.Sleep(1000);
}
catch
{
break;
}
}
}
您可以创建一个WCF服务,该服务创建到给定文件的输出,然后返回一个对象实例,该实例包含信息,例如文件中的页面数(页面大小取决于您在WCF服务中设置的缓冲区大小)。
例如,创建一个操作约定Public Function execCommand(Byval commandName作为String,Byval outputFile作为String)作为OutputInfo
然后创建第二种方法,可以逐页检索信息(如果没有更多要检索的页面或该页面超出范围,则可能返回false)
公共函数getOutput(ByVal fileName作为String,ByVal pageNo作为Integer)作为OutputPage
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.