You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WPF中映射网络驱动器?求可行方案及相关资源

在WPF中映射网络驱动器的实现方案

一、使用Windows API类库实现(推荐方案)

直接调用Windows原生API是最可靠的方式,无需依赖外部命令,可在WPF进程内完成操作,还能精准处理返回状态与错误信息。

核心是调用WNetAddConnection2这个Windows API函数,它专门用于建立网络连接(包括映射驱动器),你可以通过P/Invoke在C#中封装该方法:

示例代码

using System;
using System.Runtime.InteropServices;

public class NetworkDriveMapper
{
    // 定义WNetAddConnection2所需的结构体
    [StructLayout(LayoutKind.Sequential)]
    private struct NETRESOURCE
    {
        public uint dwScope;
        public uint dwType;
        public uint dwDisplayType;
        public uint dwUsage;
        public string lpLocalName;
        public string lpRemoteName;
        public string lpComment;
        public string lpProvider;
    }

    // 声明P/Invoke方法
    [DllImport("mpr.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern uint WNetAddConnection2(ref NETRESOURCE lpNetResource, string lpPassword, string lpUserName, uint dwFlags);

    // 映射驱动器的公开方法
    public static uint MapDrive(string driveLetter, string networkPath, string username = null, string password = null)
    {
        NETRESOURCE nr = new NETRESOURCE();
        nr.dwType = 0x00000001; // 指定为磁盘资源类型
        nr.lpLocalName = driveLetter + ":";
        nr.lpRemoteName = networkPath;

        // 0表示无特殊标志,若需重启后保留映射,可传入0x00000001(CONNECT_UPDATE_PROFILE)
        return WNetAddConnection2(ref nr, password, username, 0);
    }

    // 取消映射的方法(可选)
    [DllImport("mpr.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern uint WNetCancelConnection2(string lpName, uint dwFlags, bool fForce);

    public static uint UnmapDrive(string driveLetter)
    {
        return WNetCancelConnection2(driveLetter + ":", 0, true);
    }
}

使用方式

在WPF的ViewModel或后台代码中直接调用:

// 映射Z盘到\\server\sharedFolder
uint result = NetworkDriveMapper.MapDrive("Z", @"\\server\sharedFolder");
if (result == 0)
{
    // 映射成功逻辑
}
else
{
    // 调用Marshal.GetLastWin32Error()获取详细错误码,进行针对性处理
}

该方案优势:

  • 进程内执行,无需启动外部命令行进程
  • 可直接获取Windows API返回码,精准处理权限不足、路径不存在等错误
  • 支持带用户名密码的网络共享映射
  • 通过CONNECT_UPDATE_PROFILE参数可实现重启后保留映射

二、通过命令行实现(备选方案)

若不想编写P/Invoke代码,也可通过WPF调用Cmd或PowerShell命令完成映射:

Cmd命令方式

使用net use命令,通过Process类执行:

using System.Diagnostics;

public static void MapDriveViaCmd(string driveLetter, string networkPath)
{
    ProcessStartInfo psi = new ProcessStartInfo
    {
        FileName = "cmd.exe",
        Arguments = $"/c net use {driveLetter}: {networkPath}",
        WindowStyle = ProcessWindowStyle.Hidden,
        CreateNoWindow = true
    };
    Process.Start(psi).WaitForExit();
}

PowerShell方式

使用New-PSDrive命令,适合复杂逻辑场景:

public static void MapDriveViaPowerShell(string driveLetter, string networkPath)
{
    ProcessStartInfo psi = new ProcessStartInfo
    {
        FileName = "powershell.exe",
        Arguments = $"New-PSDrive -Name {driveLetter} -PSProvider FileSystem -Root {networkPath} -Persist",
        WindowStyle = ProcessWindowStyle.Hidden,
        CreateNoWindow = true
    };
    Process.Start(psi).WaitForExit();
}

该方案劣势:无法直接获取详细错误信息,只能通过进程退出码判断,且依赖外部进程,稳定性不如直接调用API。


内容的提问来源于stack exchange,提问作者Šimon Cerman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 21:12:22