将非法文件名字符替换为有效字符



我正在开发一个视频下载器,其中一些标题中有非法字符。我已经尝试用非非法字符替换非法字符,但没有奏效。无论我做什么,我都会收到此错误:"不支持给定路径的格式。

这是我当前的代码:

var videoDownloader = new VideoDownloader(video, Path.Combine(path, filename + video.VideoExtension));
            string invalid = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
            videoDownloader.DownloadProgressChanged += (sender, args) => Console.WriteLine("Video " + args.ProgressPercentage + "% downloaded...");
            string invalidChars = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
        foreach (char c in invalidChars)
        {
            filename = filename.Replace(c.ToString(), "."); // or with "."
        }
        videoDownloader.Execute(); // where the error occurs.
        foreach (char c in invalidChars)
        {
            filename = filename.Replace(c.ToString(), "."); // or with "."
        }

我可以通过添加自定义文件名轻松解决此问题,但我更喜欢使用视频中的原始文件名。

另外要注意的是,我正在使用这个库:https://www.nuget.org/packages/YoutubeExtractor

您正在交VideoDownloader旧的未固定文件名。 您需要在使用字符串之前替换坏字符,而不是之后。

最新更新