Here's the scenario.
这是场景。
I'm calling a database and pulling in around ~3000 records which are child nodes of parent documents, with these child nodes I'm working back up the tree to generate a flat folder name which looks something like this:
我调用了一个数据库,并在大约3000条记录中拉入了父文档的子节点,这些子节点我正在返回树中生成一个扁平的文件夹名,它看起来是这样的:
Entertainment Categories ~ Artistic Entertainment ~ Calligraphy Artists ~ Megumi - Japanese Calligraphy Artist Sussex ~ 6546
娱乐类~艺术娱乐~书法艺术家~大美-日本书法艺术家苏塞克斯~ 6546。
Going from parent all the way down to child and then child ID.
从父母一直到孩子,然后是儿童ID。
What I then do is iterate over these folder names and create a new folder using the name, this all works fine, I'm running the names through a loop using Path.GetInvalidFileNameChars to get rid of any characters that would prevent the folder creation from failing.
然后,我将遍历这些文件夹名称并使用名称创建一个新的文件夹,这一切正常,我使用Path来运行这些名称。GetInvalidFileNameChars删除任何可以防止文件夹创建失败的字符。
However, when it comes to zipping these folders up using the built in zip function in windows (right click > send to > compressed zip folder) I keep getting errors:
但是,当你使用windows中内置的zip函数来压缩这些文件夹时(右击>发送到>压缩zip文件夹),我总是会出错:
[Folder name] cannot be compressed because it includes characters that cannot be used in a compressed folder, such as [foo]. You should rename this file or directory.
不能被压缩,因为它包含不能在压缩文件夹中使用的字符,例如[foo]。您应该重命名这个文件或目录。
It would be fine if the error message actually told me the range of characters that cannot be included in folder names but it doesn't, so whenever I do a replace on the character I get a new one pop up in the error message, this is what I'm doing to clean the folder name at the moment:
实际上会好如果错误消息的字符范围告诉我,不能包含在文件夹名字但没有,所以每当我做一个替换字符我得到一个新的弹出错误信息,这就是我在做清洁目前文件夹名称:
private static void CleanPath(StringBuilder path)
{
List<string> invalidFolderCharacters = Path.GetInvalidFileNameChars()
.Select(x => x.ToString()).ToList();
invalidFolderCharacters.Add("–");
invalidFolderCharacters.Add("`");
invalidFolderCharacters.Add("\'");
invalidFolderCharacters.Add("′");
foreach (string s in invalidFolderCharacters)
{
path.Replace(s, string.Empty);
}
}
As you can see, I'm having to add to the characters returned by GetInvalidFileNameChars()
each time a new error pops up.
正如您所看到的,每当出现新的错误时,我必须添加GetInvalidFileNameChars()返回的字符。
So my question is - Is there a built in function in the .NET framework that I can use which will provide me with characters that aren't allowed in file/folder names as well as characters that cannot be in compressed folder names? Can anyone tell me what characters aren't allowed in compressed folder names so that I can create one myself?
所以我的问题是,在。net框架中是否有一个内置的函数,我可以使用它来给我提供不允许在文件/文件夹名称中以及在压缩文件夹名称中不能使用的字符?有没有人能告诉我压缩文件夹里哪些字符是不允许的,这样我就可以自己创建一个?
3 个解决方案
#2
0
One reason you might get this message is because of Unicode characters. However, trying to figure out if a string is Unicode from your program will be a challenge. See When is a string not a string?
您可能会得到此消息的一个原因是Unicode字符。但是,试图弄清楚一个字符串是否从您的程序中获得Unicode将是一个挑战。看什么时候字符串不是字符串?
I would try to PInvoke the native IsTextUnicode function.
我将尝试调用本机IsTextUnicode函数。
Here a couple other suggestions:
https://*.com/a/4459679/2596334
https://*.com/a/1522897/2596334
这里有一些其他的建议:https://*.com/a/4459679/2596334 https://*.com/a/1522897/2596334。
#3
-1
This is pulled from a JavaScript project I wrote, which was originally compiled from this Wikipedia article:
这是从我写的一个JavaScript项目中提取出来的,它最初是由*上的文章编译的:
var contain = ['"', '*', ':', '<', '>', '?', '\\', '/', '|', '+', '[', ']'];
var fullname = ['AUX', 'COM1', 'COM2', 'COM3', 'COM4', 'COM5', 'COM6', 'COM7', 'COM8', 'COM9', 'CON', 'LPT1', 'LPT2', 'LPT3', 'LPT4', 'LPT5', 'LPT6', 'LPT7', 'LPT8', 'LPT9', 'NUL', 'PRN'];
Filenames cannot contain any of the characters in the first var
, and cannot be named exactly the same as any of the names in the second var
.
文件名不能包含第一个var中的任何字符,不能与第二个var中的任何名称完全相同。
#1
#2
0
One reason you might get this message is because of Unicode characters. However, trying to figure out if a string is Unicode from your program will be a challenge. See When is a string not a string?
您可能会得到此消息的一个原因是Unicode字符。但是,试图弄清楚一个字符串是否从您的程序中获得Unicode将是一个挑战。看什么时候字符串不是字符串?
I would try to PInvoke the native IsTextUnicode function.
我将尝试调用本机IsTextUnicode函数。
Here a couple other suggestions:
https://*.com/a/4459679/2596334
https://*.com/a/1522897/2596334
这里有一些其他的建议:https://*.com/a/4459679/2596334 https://*.com/a/1522897/2596334。
#3
-1
This is pulled from a JavaScript project I wrote, which was originally compiled from this Wikipedia article:
这是从我写的一个JavaScript项目中提取出来的,它最初是由*上的文章编译的:
var contain = ['"', '*', ':', '<', '>', '?', '\\', '/', '|', '+', '[', ']'];
var fullname = ['AUX', 'COM1', 'COM2', 'COM3', 'COM4', 'COM5', 'COM6', 'COM7', 'COM8', 'COM9', 'CON', 'LPT1', 'LPT2', 'LPT3', 'LPT4', 'LPT5', 'LPT6', 'LPT7', 'LPT8', 'LPT9', 'NUL', 'PRN'];
Filenames cannot contain any of the characters in the first var
, and cannot be named exactly the same as any of the names in the second var
.
文件名不能包含第一个var中的任何字符,不能与第二个var中的任何名称完全相同。