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

如何不将文件添加到项目中,打开指定目录(含局域网)的媒体文件

局域网共享文件播放实现方案

核心前提:使用UNC路径

局域网文件不能用本地盘符路径(如C:/User/Folder/video.mp4),必须换成UNC格式路径:\\目标机器名/IP\共享目录路径\文件名。
比如目标机器IP为192.168.1.100,且C:\User已设为共享目录,那么正确路径是:

\\192.168.1.100\User\Folder\video.mp4

方案1:直接访问(有权限情况下)

如果当前系统账号已被授权访问目标共享文件,可直接用FileStream读取:

var uncPath = @"\\192.168.1.100\User\Folder\video.mp4";

if (File.Exists(uncPath))
{
    using (var stream = new FileStream(uncPath, FileMode.Open, FileAccess.Read))
    {
        mediaElement.SetSource(stream, "video/mp4");
        mediaElement.Play(); // 可选:触发播放
    }
}

方案2:带凭据访问(权限不足时)

如果当前账号无访问权限,可通过添加网络凭据的方式访问,需要实现一个辅助类处理网络连接:

辅助类代码

public class NetworkConnection : IDisposable
{
    private readonly string _networkPath;

    public NetworkConnection(string networkPath, NetworkCredential credentials)
    {
        _networkPath = networkPath;
        var netResource = new NetResource
        {
            Scope = ResourceScope.GlobalNetwork,
            ResourceType = ResourceType.Disk,
            DisplayType = ResourceDisplaytype.Share,
            RemoteName = networkPath
        };

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

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

    [DllImport("mpr.dll")]
    private static extern int WNetAddConnection2(NetResource netResource, string password, string username, int flags);

    [DllImport("mpr.dll")]
    private static extern int WNetCancelConnection2(string name, int flags, bool force);
}

[StructLayout(LayoutKind.Sequential)]
public class NetResource
{
    public ResourceScope Scope;
    public ResourceType ResourceType;
    public ResourceDisplaytype DisplayType;
    public int Usage;
    public string LocalName;
    public string RemoteName;
    public string Comment;
    public string Provider;
}

public enum ResourceScope : int
{
    Connected = 1,
    GlobalNetwork,
    Remembered,
    Recent,
    Context
}

public enum ResourceType : int
{
    Any = 0,
    Disk = 1,
    Print = 2,
    Reserved = 8,
}

public enum ResourceDisplaytype : int
{
    Generic = 0x0,
    Domain = 0x01,
    Server = 0x02,
    Share = 0x03,
    File = 0x04,
    Group = 0x05,
    Network = 0x06,
    Root = 0x07,
    Shareadmin = 0x08,
    Directory = 0x09,
    Tree = 0x0a,
    Ndscontainer = 0x0b
}

调用代码

var uncPath = @"\\192.168.1.100\User\Folder\video.mp4";
var credentials = new NetworkCredential("目标共享账号", "目标共享密码");

using (new NetworkConnection(uncPath, credentials))
{
    if (File.Exists(uncPath))
    {
        using (var stream = new FileStream(uncPath, FileMode.Open, FileAccess.Read))
        {
            mediaElement.SetSource(stream, "video/mp4");
            mediaElement.Play();
        }
    }
}

注意事项

  • 确保目标机器已开启文件共享,且目标文件夹给访问账号分配了读取权限
  • 先通过Windows资源管理器测试UNC路径是否能正常打开,排除网络或权限问题
  • 若为UWP应用,需在Package.appxmanifest中添加Private Networks (Client & Server)权限

内容的提问来源于stack exchange,提问作者Chārry

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 05:45:42