Reputation: 14608
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:
Entertainment Categories ~ Artistic Entertainment ~ Calligraphy Artists ~ Megumi - Japanese Calligraphy Artist Sussex ~ 6546
Going from parent all the way down to child and then child 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.
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:
[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.
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.
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?
Upvotes: 4
Views: 40947
Reputation: 3897
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?
I would try to PInvoke the native IsTextUnicode function.
Here a couple other suggestions:
https://stackoverflow.com/a/4459679/2596334
https://stackoverflow.com/a/1522897/2596334
Upvotes: 0
Reputation: 20850
This is pulled from a JavaScript project I wrote, which was originally compiled from this Wikipedia article:
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
.
Upvotes: -1