記得要引入 Windows Script Host Object Model參考
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using IWshRuntimeLibrary; //Add "Windows Script Host Object Model" COM reference using System.IO; using System.Collections; namespace Mount_Data { class NetworkDriver { private WshNetwork _networkShell = new WshNetwork(); private void MapDrive(string driveLetter, string UNC, bool isPersistent, string userName, string password) { if (driveLetter != "") { DisconnectDrive(driveLetter, true, true); } else { DisconnectDrive(UNC, true, true); } object persistent = isPersistent; object user = userName; object pwd = password; _networkShell.MapNetworkDrive(driveLetter, UNC, ref persistent, ref user, ref pwd); } private void DisconnectDrive(string UNC_or_DriveName, bool willForce, bool isPersistent) { try { object force = willForce; object updateProfile = isPersistent; _networkShell.RemoveNetworkDrive(UNC_or_DriveName, ref force, ref updateProfile); } catch { // } } /// <summary> /// Mount Remote disk /// </summary> /// <param name="sour_path">remote source path , ex: @"file://123.456.789.123/qq" </param> /// <param name="DriveLable">remote disk lable , ex: X, Y ,Z </param> /// <param name="user_id"></param> /// <param name="password"></param> public void MAP_DRIVER(String sour_path, string DriveLable,string user_id,string password) { MapDrive(DriveLable+":", sour_path, false, user_id, password); } /// <summary> /// Umount /// </summary> public void DIS_MAP_DRIVER(string DriveLable) { DisconnectDrive(DriveLable+":", true, true); } public char getFreeLabel() { char letter = ' '; ArrayList driveLetters = new ArrayList(26); // Allocate space for alphabet for (int i = 65; i < 91; i++) // increment from ASCII values for A-Z { driveLetters.Add(Convert.ToChar(i)); // Add uppercase letters to possible drive letters } foreach (string drive in Directory.GetLogicalDrives()) { driveLetters.Remove(drive[0]); // removed used drive letters from possible drive letters } foreach (char drive in driveLetters) { //comboDrives.Items.Add(drive); // add unused drive letters to the combo box letter = drive; break; } return letter; } } }
記得先去一個空的磁碟機代號,可以參考這篇 ,以下是使用範例。
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; namespace WindowsFormsApplication2 { public partial class Form1 : Form { //«Å§i¤@ÓNetworkDiriverª«¥ó¡A¥Î¨Ó±¾¸ü»·ºÝºÏºÐ NetworkDriver nd = new NetworkDriver(); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //UNC format \\IP\folder , don't use \\IP\folder\\ nd.MAP_DRIVER(@"\\192.168.1.109\個人資料夾","z","username","password"); } private void button2_Click(object sender, EventArgs e) { nd.DIS_MAP_DRIVER("z"); } } }
注意一下,遠端磁碟是採用一種叫UNC的路徑格式, \\IP\\Folder ,
不要多打了一個\ ,變成\\IP\\Folder\\ ,會連到Timerout。
按下Mount後,會Mount指定的遠端磁碟,按下Umount會中斷連接
按下Mount後會出出一個遠端磁碟
按下Umount之後會消失
沒有留言:
張貼留言