问题描述
通过连频带凑,总算能远程读取已安装文件列表,求教怎样远程删除已选中的读取到的软件usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingSystem.Management;usingSystem.Threading;usingSystem.Net.NetworkInformation;namespace远程软件监控{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidbtnGetInfo_Click(objectsender,EventArgse){listBox1.Items.Clear();Threadth=newThread(GetSoftwareInfo);th.IsBackground=true;th.Start();}List<string>list=newList<string>();privatevoidGetSoftwareInfo(){ConnectionOptionsConn=newConnectionOptions();Conn.Username=tbxName.Text;Conn.Password=tbxPwd.Text;ManagementScopeMs=newManagementScope("\\"+tbxPCName.Text+"\root\cimv2",Conn);if(IsMachineUp(tbxPCName.Text)==true){Ms.Connect();stringsoftwareRegLoc=@"SoftwareMicrosoftWindowsCurrentVersionUninstall";ManagementClassregistry=newManagementClass(Ms,newManagementPath("StdRegProv"),null);//StdRegProvisWMIclassManagementBaseObjectinParams=registry.GetMethodParameters("EnumKey");//EnumKey方法有两个输入参数(hDefKey,sSubKeyName)和两个输出参数(ReturnValue,sNames[])inParams["hDefKey"]=0x80000002;//HKEY_CLASSES_ROOT(2147483648(0x80000000))//HKEY_CURRENT_USER(2147483649(0x80000001))//HKEY_LOCAL_MACHINE(2147483650(0x80000002))//HKEY_USERS(2147483651(0x80000003))//HKEY_CURRENT_CONFIG(2147483653(0x80000005))inParams["sSubKeyName"]=softwareRegLoc;//hDefKey+sSubKeyName组成完整路径HKEY_LOCAL_MACHINESoftwareMicrosoftWindowsCurrentVersionUninstall//sNames即为Uninstall下面的所有子路径名,sNames[]输出为数组,要用数组来接受数据ManagementBaseObjectoutParams=registry.InvokeMethod("EnumKey",inParams,null);//上述用法见https://msdn.microsoft.com/zh-cn/library/ssk42c11(v=vs.100).aspxstring[]programGuids=outParams["sNames"]asstring[];foreach(stringsubKeyNameinprogramGuids){inParams=registry.GetMethodParameters("GetStringValue");//GetStringValue方法有4个参数,三个输入参数,一个输出参数//[in] uint32hDefKey=2147483650,hDefKey=2147483650等同于hDefKey=0x80000002//[in] stringsSubKeyName,//[in] stringsValueName,//[out] stringsValueinParams["hDefKey"]=0x80000002;inParams["sSubKeyName"]=softwareRegLoc+@""+subKeyName;inParams["sValueName"]="DisplayName";//ReadRegistryValueoutParams=registry.InvokeMethod("GetStringValue",inParams,null);if(outParams.Properties["sValue"].Value!=null){stringsoftwareName=outParams.Properties["sValue"].Value.ToString();//programs.Add(softwareName);try{listBox1.Items.Add(softwareName);list.Add(softwareName);}catch(Exceptionex){}}}}else{MessageBox.Show("当前电脑未在线或不存在");}}privatestaticboolIsMachineUp(stringhostName){boolretVal=false;try{PingpingSender=newPing();PingOptionsoptions=newPingOptions();//UsethedefaultTtlvaluewhichis128,//butchangethefragmentationbehavior.options.DontFragment=true;//Createabufferof32bytesofdatatobetransmitted.stringdata="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";byte[]buffer=Encoding.ASCII.GetBytes(data);inttimeout=120;PingReplyreply=pingSender.Send(hostName,timeout,buffer,options);if(reply.Status==IPStatus.Success){retVal=true;}}catch(Exceptionex){retVal=false;Console.WriteLine(ex.Message);}returnretVal;}privatevoidForm1_Load(objectsender,EventArgse){Control.CheckForIllegalCrossThreadCalls=false;}privatevoidbtnUninstall_Click(objectsender,EventArgse){stringproductName=list[listBox1.SelectedIndex];}}}
解决方案
解决方案二:
换张图