Mount Network Drive with WMI

2019-03-04 08:04发布

问题:

Trying to write a WMI class function to mount a network drive on any computer (remote or local) using the credentials of the logged in computer.

This is a class for a larger project that I wrote for help desk staff to do first line fixes on remote PC's. The tech types in the the machine name or ip address and the app connects to it and allows to tech to click a couple of buttons and fix some basic items without having to remote(VNC) into the PC.

I've read all over the internet that it is much easier ways than WMI, but due to the remote nature of the app I would rather not use local API calls, nor do I want to worry about uploading script and executing it though a process start. Also other functions are already in WMI so I'd like to keep the code base the same.

The basic idea is to mount H: to //fileserver.example.com/$username

NetFixer is already in production use so I'm trying to keep my code nice and neat

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;

namespace WMIcontrols
{
  public class Remote
  {

   public string target;

   //Some code skipped here for simplicity sake... 

   public bool MountNetDrive(string DriveLetter, string MountLocation)
    {
      try
      {
          //Mount the network drive
          return true;
      }
      catch
      {
          //Mount Failed
          return false;
      }
    }
  }
}

回答1:

This is not using WMI but will accomplish what you want and is very simple

System.Diagnostics.Process.Start("cmd", "/c net use x: \\fileserver.example.com /user:Username Password");