How to remote computer using WMI in c#

MIPAKTEH_1 605 Reputation points
2023-11-22T10:28:24.4533333+00:00

Hi All,

I want to remote from laptop to desktop.I test this code.No error.

how to access the desktop compuuter.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Management;
using System.Management.Instrumentation;


namespace WMI_
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ConnectionOptions options = new ConnectionOptions();

            options.Username = "DESKTOP-Q5T7P5C";
            options.Password = "";
            options.Authority = "ntlmdomain:192.168.0.139";

            ManagementScope scope = new ManagementScope("\\\\LAPTOP-1087EDI9\\root\\cimv2");
            //ManagementScope scope = new ManagementScope("\\\\192.168.0.199\\root\\cimv2");
            scope.Options.EnablePrivileges = true;
            scope.Connect();
        }
    }
}

Developer technologies C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2023-11-23T03:08:44.4533333+00:00

    Hi @MIPAKTEH_1 , Welcome to Microsoft Q&A,

    If you can connect correctly.

    You can use the System.Diagnostics.Process class to start the calculator.

    private void Form1_Load(object sender, EventArgs e)
    {
         ConnectionOptions options = new ConnectionOptions();
         options.Username = "your_remote_username";
         options.Password = "your_remote_password";
         options.Authority = "ntlmdomain:your_domain";
    
         string remoteComputer = "192.168.0.199"; // Replace with the IP address or name of your remote computer
    
         ManagementScope scope = new ManagementScope($"\\\\{remoteComputer}\\root\\cimv2", options);
         scope.Options.EnablePrivileges = true;
    
         ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Process");
    
         using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
         {
             ManagementClass processClass = new ManagementClass(scope, new ManagementPath("Win32_Process"), new ObjectGetOptions());
             ManagementBaseObject inParams = processClass.GetMethodParameters("Create");
    
             //Set the process path to be started
             inParams["CommandLine"] = "calc.exe";
    
             //Start the calculator process
             ManagementBaseObject outParams = processClass.InvokeMethod("Create", inParams, null);
            
             // Get process ID
             uint processId = (uint)outParams["processId"];
            
             Console.WriteLine($"Calculator process started with Process ID: {processId}");
         }
    }
    

    You need to make sure the exe path is correct. Also make sure to replace your_remote_username, your_remote_password and your_domain with your actual credentials and domain name of your remote computer, and replace remoteComputer with the IP address or name of the remote computer you want to connect to.

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.