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

.NET Core 3.1如何获取另一台服务器的目录与文件列表?

在.NET Core 3.1中访问远程服务器目录的解决方案

问题根源分析

  1. 路径格式错误:你使用的@"\someServer\c$\folder\folder\"是不合法的UNC(通用命名约定)路径,正确的UNC路径必须以双反斜杠\\开头,单反斜杠开头会导致路径解析异常,这就是你遇到"异常的c"的原因。
  2. Server.MapPath不适用:Server.MapPath是ASP.NET Framework中的方法,用于映射本地站点的虚拟路径到物理路径,本身就不适合访问远程服务器,且在.NET Core中已被移除。

修正后的基础实现

只要路径格式正确且当前运行账号拥有远程服务器目录的访问权限,直接使用DirectoryInfo或Directory类即可,和.NET Framework中的用法一致:

// 正确的UNC路径格式:\\服务器名\共享名\目录路径
string serverPath = @"\\someServer\c$\folder\folder\";

// 先检查路径是否可访问(可选,避免直接抛出异常)
if (Directory.Exists(serverPath))
{
    var serverDirectory = new DirectoryInfo(serverPath);
    var directories = serverDirectory.GetDirectories();
    
    // 遍历并处理获取到的目录列表
    foreach (var dir in directories)
    {
        Console.WriteLine(dir.Name);
    }
}
else
{
    Console.WriteLine("路径不存在或无访问权限");
}

权限问题的解决方案

如果当前运行程序的账号没有远程服务器的访问权限(比如访问c$这类管理员共享需要管理员权限),可以通过带凭据的网络连接来访问:

1. 实现网络连接工具类

using System;
using System.ComponentModel;
using System.Net;
using System.Runtime.InteropServices;

public class NetworkConnection : IDisposable
{
    private readonly string _remotePath;

    [DllImport("mpr.dll")]
    private static extern int WNetAddConnection2(
        NETRESOURCE lpNetResource,
        string lpPassword,
        string lpUserName,
        int dwFlags);

    [DllImport("mpr.dll")]
    private static extern int WNetCancelConnection2(
        string lpName,
        int dwFlags,
        bool fForce);

    [StructLayout(LayoutKind.Sequential)]
    private class NETRESOURCE
    {
        public int dwScope = 0;
        public int dwType = 1; // 磁盘资源类型
        public int dwDisplayType = 0;
        public int dwUsage = 0;
        public string lpLocalName = "";
        public string lpRemoteName;
        public string lpComment = "";
        public string lpProvider = "";
    }

    public NetworkConnection(string remotePath, NetworkCredential credentials)
    {
        _remotePath = remotePath;
        var netResource = new NETRESOURCE { lpRemoteName = remotePath };

        int result = WNetAddConnection2(netResource, credentials.Password, credentials.UserName, 0);
        if (result != 0)
        {
            throw new Win32Exception(result);
        }
    }

    public void Dispose()
    {
        WNetCancelConnection2(_remotePath, 0, true);
    }
}

2. 使用凭据访问远程目录

// 远程服务器的账号凭据(如果是域账号,需指定域名)
var credentials = new NetworkCredential("远程用户名", "远程密码", "域名");

// using块确保使用后自动断开网络连接
using (new NetworkConnection(@"\\someServer\c$", credentials))
{
    string serverPath = @"\\someServer\c$\folder\folder\";
    var serverDirectory = new DirectoryInfo(serverPath);
    var directories = serverDirectory.GetDirectories();
    
    // 处理目录列表
    foreach (var dir in directories)
    {
        Console.WriteLine(dir.FullName);
    }
}

关键注意事项

  • UNC路径规范:必须严格遵循\\服务器名称或IP\共享目录\子目录的格式,共享目录可以是默认的管理员共享(如c$、d$)或自定义共享。
  • 权限配置:确保运行程序的账号(IIS应用池账号/控制台程序运行账号)拥有远程目录的读取权限,管理员共享需要远程服务器的管理员权限。
  • 资源释放:使用带凭据的网络连接时,务必通过IDisposable自动断开连接,避免残留网络连接。

内容的提问来源于stack exchange,提问作者Andy5

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 03:01:49