سؤال

In .NET, I'd use System.IO.Path.GetInvalidFileNameChars to scan a string containing a filename to validate it does not contain invalid characters.

In WinJS, I can't find the equivalent to this function. Is there somewhere else that these characters could be retrieved? My code is building a string based on some metadata from a file, and I suspect that there may be characters present that will not work when creating a new file (imagine like the "title" of a Microsoft Word docx file for example, where the title could contain : and \ characters that wouldn't be valid file names). I'd like to remove or convert these invalid characters so that the file can be saved without user intervention.

Of course, I could just grab a list from .NET and hard code that, but that doesn't seem nearly as elegant.

My other thought was to expose the functionality from a C# component (and expose it as a WinRT component), but I'd rather not introduce a second run-time just for some simple functions like this if I could help it.

هل كانت مفيدة؟

المحلول

There doesn't seem to be a method in WinJS that does this.

Given the small number of special characters, I'd just hardcode those. To get the list, simply try to rename a file to file?.txt in Windows Explorer. It shows you the list of invalid characters:

\ / : * ? " < > |
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top