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

如何用libVLC列出网络共享中的所有文件(不限于音视频)

问题解答

libVLC的Media类是专门为媒体文件处理设计的,它只会识别并加载自身支持的音视频/流媒体类型,没有任何标志或配置能让它扫描网络共享中的所有文件(比如txt、docx等非媒体文件)——因为它的解析逻辑核心是媒体格式识别,而非通用文件系统遍历。

如果你仅需要获取网络共享中文件的原始字节内容,完全不需要依赖libVLC,直接使用.NET原生的文件系统API即可,这是更高效、更贴合需求的方案:

方案1:直接遍历网络共享(有权限情况下)

使用System.IO命名空间下的API直接访问网络共享路径(格式如\\server\share):

string networkSharePath = @"\\your-server\your-shared-folder";

// 遍历共享目录下所有文件(包括子目录)
foreach (string filePath in Directory.EnumerateFiles(networkSharePath, "*.*", SearchOption.AllDirectories))
{
    Console.WriteLine($"找到文件: {filePath}");
    // 读取文件原始字节
    byte[] fileBytes = File.ReadAllBytes(filePath);
    // 此处可添加对字节内容的处理逻辑
}

方案2:指定访问凭证(无默认权限时)

如果运行程序的用户没有默认访问该网络共享的权限,可以通过Windows API添加网络连接并指定凭证:

首先实现一个简单的NetworkConnection类:

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Security;

public class NetworkConnection : IDisposable
{
    private readonly string _networkName;

    public NetworkConnection(string networkName, NetworkCredential credentials)
    {
        _networkName = networkName;

        var netResource = new NetResource
        {
            Scope = ResourceScope.GlobalNetwork,
            ResourceType = ResourceType.Disk,
            DisplayType = ResourceDisplaytype.Share,
            RemoteName = networkName
        };

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

    public void Dispose()
    {
        WNetCancelConnection2(_networkName, 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)]
    private 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;
    }

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

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

    private 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
    }
}

然后使用这个类来建立连接并访问共享:

string networkSharePath = @"\\your-server\your-shared-folder";
var credentials = new NetworkCredential("username", "password", "domain");

using (new NetworkConnection(networkSharePath, credentials))
{
    // 遍历并读取文件的逻辑同方案1
    foreach (string filePath in Directory.EnumerateFiles(networkSharePath, "*.*", SearchOption.AllDirectories))
    {
        byte[] fileBytes = File.ReadAllBytes(filePath);
        Console.WriteLine($"读取文件: {filePath}, 字节数: {fileBytes.Length}");
    }
}

总结

libVLC不是通用文件系统扫描工具,你的需求完全可以通过.NET原生IO API实现,无需依赖libVLC的媒体处理能力,这样更简单高效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 21:43:24