使用c#远程安装程序 [英] install program remotely using c#
本文介绍了使用c#远程安装程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好。
我将使用c#应用程序远程安装程序。
但是,我的代码不起作用。
有人帮忙这个代码吗?
使用Microsoft.Win32;
使用System;
使用System.Management;
使用System.Runtime.InteropServices;使用System.ServiceProcess
;
命名空间WMITest
{
class clsMain
{
static void Main(string [] args)
{
RemoteMSI( "ipaddress","msi program path","property = setting","username","userpassword","domain",";
Console.ReadKey();
}
public static void RemoteMSI(string machine,string msi,string commandline,string username,string password,string domain)
{
try
{
ConnectionOptions connection = new ConnectionOptions();
connection.Username = username;
connection.Password =密码;
ConnectionOptions options = new ConnectionOptions();
string targetIpAddress = machine;
connection.Authentication = System.Management.AuthenticationLevel.Packet;
ManagementScope scope = new ManagementScope((" \\\\ quot; + targetIpAddress),options);
scope.Connect();
ManagementPath p = new ManagementPath(" Win32_Product");
ManagementClass classInstance = new ManagementClass(scope,p,null);
ManagementBaseObject inParams = classInstance.GetMethodParameters(" Install");
inParams [" AllUsers"] = true;
inParams [" Options"] = string.Empty;
inParams [" PackageLocation"] =" \\\\" + targetIpAddress +" msi program path" //安装程序存储在此位置
ManagementBaseObject outParams = classInstance.InvokeMethod(" Install",inParams,null);
string retVal = outParams [" ReturnValue"]。ToString();
Console.WriteLine(retVal);
}
catch(ManagementException me)
{
Console.WriteLine(me.Message);
}
catch(COMException ioe)
{
Console.WriteLine(ioe.Message);
}
}
}
}
解决方案
< blockquote>
我已经解决了这个问题。
参见下面的代码
public static void Install(String _server, String _user,String _password,String _path)
{
try
{
ManagementScope scope;
ConnectionOptions connectionOptions = new ConnectionOptions
{
Authentication = AuthenticationLevel.PacketPrivacy,
Impersonation = ImpersonationLevel.Impersonate
};
connectionOptions = new ConnectionOptions();
connectionOptions.Impersonation = ImpersonationLevel.Impersonate;
connectionOptions.EnablePrivileges = true;
connectionOptions.Username = _server +" \\" + _user;
connectionOptions.Password = _password;
scope = new ManagementScope(@" \\" + _server +
@" \ ROOT \CIMV2",connectionOptions);
scope.Connect();
ManagementPath p = new ManagementPath(" Win32_Product");
ManagementClass classInstance = new ManagementClass(scope,p,null);
ManagementBaseObject inParams = classInstance.GetMethodParameters(" Install");
inParams [" AllUsers"] = true;
inParams [" Options"] = string.Empty;
inParams [" PackageLocation"] = @" \\" + _ server +" \\" + _ path;
ManagementBaseObject outParams = classInstance.InvokeMethod(" Install",inParams,null);
string retVal = outParams [" ReturnValue"]。ToString();
}
catch(ManagementException me)
{
Console.WriteLine(me.Message) );
}
catch(COMException ioe)
{
Console.WriteLine(ioe.Message);
}
}
Hello, All.
I am going to install the program remotely using c# application.
But, my code doesn't work.
Anybody to help this code?
using Microsoft.Win32; using System; using System.Management; using System.Runtime.InteropServices; using System.ServiceProcess; namespace WMITest { class clsMain { static void Main(string[] args) { RemoteMSI("ipaddress", "msi program path", "property=setting", "username","userpassword", "domain"); Console.ReadKey(); } public static void RemoteMSI(string machine, string msi, string commandline, string username, string password, string domain) { try { ConnectionOptions connection = new ConnectionOptions(); connection.Username = username; connection.Password = password; ConnectionOptions options = new ConnectionOptions(); string targetIpAddress = machine; connection.Authentication = System.Management.AuthenticationLevel.Packet; ManagementScope scope = new ManagementScope(("\\\\" + targetIpAddress), options); scope.Connect(); ManagementPath p = new ManagementPath("Win32_Product"); ManagementClass classInstance = new ManagementClass(scope, p, null); ManagementBaseObject inParams = classInstance.GetMethodParameters("Install"); inParams["AllUsers"] = true; inParams["Options"] = string.Empty; inParams["PackageLocation"] = "\\\\" + targetIpAddress + "msi program path";//installer is stored at this location ManagementBaseObject outParams = classInstance.InvokeMethod("Install", inParams, null); string retVal = outParams["ReturnValue"].ToString(); Console.WriteLine(retVal); } catch (ManagementException me) { Console.WriteLine(me.Message); } catch (COMException ioe) { Console.WriteLine(ioe.Message); } } } }
解决方案
I already resolved this problem.
See the below code
public static void Install(String _server,String _user,String _password,String _path) { try { ManagementScope scope; ConnectionOptions connectionOptions = new ConnectionOptions { Authentication = AuthenticationLevel.PacketPrivacy, Impersonation = ImpersonationLevel.Impersonate }; connectionOptions = new ConnectionOptions(); connectionOptions.Impersonation = ImpersonationLevel.Impersonate; connectionOptions.EnablePrivileges = true; connectionOptions.Username = _server + "\\" + _user; connectionOptions.Password = _password; scope = new ManagementScope(@"\\" + _server + @"\ROOT\CIMV2", connectionOptions); scope.Connect(); ManagementPath p = new ManagementPath("Win32_Product"); ManagementClass classInstance = new ManagementClass(scope, p, null); ManagementBaseObject inParams = classInstance.GetMethodParameters("Install"); inParams["AllUsers"] = true; inParams["Options"] = string.Empty; inParams["PackageLocation"] = @"\\"+_server+ "\\"+_path; ManagementBaseObject outParams = classInstance.InvokeMethod("Install", inParams, null); string retVal = outParams["ReturnValue"].ToString(); } catch (ManagementException me) { Console.WriteLine(me.Message); } catch (COMException ioe) { Console.WriteLine(ioe.Message); } }
这篇关于使用c#远程安装程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文