简体   繁体   English

在C#中使用CreateDirectory避免NotSupportedException

[英]Avoiding NotSupportedException using CreateDirectory in c#

I'm trying to recursively create a bunch of directories and certain directory names have ':' characters in which throws the above exception. 我试图递归创建一堆目录,并且某些目录名称带有':'字符,并引发上述异常。 I was hoping there may be a way to avoid this? 我希望有办法避免这种情况? Below is a snip of the code I'm using: 以下是我使用的代码片段:

foreach (TagLib.File tagFile in tagFiles)
        {
            GetInfo(tagFile, targetDir);

            if (!Directory.Exists(TargetFullPath))
            {
                Directory.CreateDirectory(TargetFullPath);
                System.IO.File.Copy(FilePath, TargetFullPath + "\\" + tagFile.Tag.Title + TargetExt);
            } ...

Where 'TargetFullPath' = "G:\\Users\\Jon\\Desktop\\musictest\\Journey\\Journey: Greatest Hits" 其中'TargetFullPath'=“ G:\\ Users \\ Jon \\ Desktop \\ musictest \\ Journey \\ Journey:Greatest Hits”

Many Thanks :) 非常感谢 :)

Colons are one of those characters you just can't use, but you could replace it easily enough. 冒号是您无法使用的字符之一,但是您可以轻松地替换它。 To also make sure you only replace characters in the file name portion (so you don't wipe out the backslashes making up the rest of the file path), you could use: 为了确保只替换文件名部分中的字符(以免擦掉组成文件路径其余部分的反斜杠),可以使用:

Path.Combine(Path.GetDirectoryName(TargetFullPath),Path.GetFileName(TargetFullPath).Replace(":","_"));

Assuming there could be other illegal characters in the file name ( see this list ), you'll want something more robust like a Regex statement. 假设文件名中可能还有其他非法字符( 请参见此列表 ),那么您将需要更强大的功能,例如Regex语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM