Skip to main content

Notifications

Build a valid file name

I could find anything in AX building a true valid file name. That is only the name of the file itself, not the path.

So I came up with this:
public static Filename buildValidFilename(str  _filename)
{
#xppTexts
#define.LeftSquareBracket('[')
#define.RightSquareBracket(']')

Filename validFilename;
str invalidFileNameChars;

new InteropPermission(InteropKind::ClrInterop).assert();

// Remove the characters that .NET defines as invalid
invalidFileNameChars = new System.String(System.IO.Path::GetInvalidFileNameChars());
validFilename = System.Text.RegularExpressions.Regex::Replace(_filename, #LeftSquareBracket + invalidFileNameChars + #RightSquareBracket, #emptyString);

// Remove the characters that .NET doesn't remove
invalidFileNameChars = @'"\\/:*?\<>|' + "'";
validFilename = System.Text.RegularExpressions.Regex::Replace(validFilename, #LeftSquareBracket + invalidFileNameChars + #RightSquareBracket, #emptyString);

CodeAccessPermission::revertAssert();

return validFilename;
}

Comments

*This post is locked for comments