dotnet
config_error
ai_generated
true
System.ArgumentException: Illegal characters in path.
ID: dotnet/illegal-char-in-path
85%Fix Rate
85%Confidence
1Evidence
2023-03-20First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| net6.0 | active | — | — | — |
| net7.0 | active | — | — | — |
| net8.0 | active | — | — | — |
| net9.0 | active | — | — | — |
Root Cause
A file path contains characters not allowed by the operating system, such as colons, asterisks, or angle brackets, often due to user input or malformed configuration.
generic中文
文件路径包含操作系统不允许的字符,例如冒号、星号或尖括号,通常是由于用户输入或配置格式错误。
Official Documentation
https://learn.microsoft.com/en-us/dotnet/api/system.io.pathtoo longexception?view=net-8.0Workarounds
-
90% success Sanitize the path using Path.GetInvalidFileNameChars() and Path.GetInvalidPathChars() to replace or remove illegal characters. Example: 'string safePath = string.Concat(path.Where(c => !Path.GetInvalidPathChars().Contains(c) && !Path.GetInvalidFileNameChars().Contains(c)));'
Sanitize the path using Path.GetInvalidFileNameChars() and Path.GetInvalidPathChars() to replace or remove illegal characters. Example: 'string safePath = string.Concat(path.Where(c => !Path.GetInvalidPathChars().Contains(c) && !Path.GetInvalidFileNameChars().Contains(c)));'
-
85% success Use Path.Combine() to build paths safely instead of string concatenation. For user input, validate with a regex like '^[a-zA-Z0-9_\\-./\\s]+$' and reject invalid input early.
Use Path.Combine() to build paths safely instead of string concatenation. For user input, validate with a regex like '^[a-zA-Z0-9_\\-./\\s]+$' and reject invalid input early.
-
80% success Wrap file path operations in try-catch for ArgumentException and log the path for debugging. Then, implement a fallback like using a GUID-based file name: 'string safeFileName = Guid.NewGuid().ToString() + Path.GetExtension(originalPath);'
Wrap file path operations in try-catch for ArgumentException and log the path for debugging. Then, implement a fallback like using a GUID-based file name: 'string safeFileName = Guid.NewGuid().ToString() + Path.GetExtension(originalPath);'
中文步骤
Sanitize the path using Path.GetInvalidFileNameChars() and Path.GetInvalidPathChars() to replace or remove illegal characters. Example: 'string safePath = string.Concat(path.Where(c => !Path.GetInvalidPathChars().Contains(c) && !Path.GetInvalidFileNameChars().Contains(c)));'
Use Path.Combine() to build paths safely instead of string concatenation. For user input, validate with a regex like '^[a-zA-Z0-9_\\-./\\s]+$' and reject invalid input early.
Wrap file path operations in try-catch for ArgumentException and log the path for debugging. Then, implement a fallback like using a GUID-based file name: 'string safeFileName = Guid.NewGuid().ToString() + Path.GetExtension(originalPath);'
Dead Ends
Common approaches that don't work:
-
Hardcoding the path with a different separator (e.g., using '/' instead of '\')
70% fail
The issue is not the separator but illegal characters like ':', '?', '*' which are invalid on Windows regardless of separator.
-
Using Path.GetInvalidPathChars() to remove characters but only checking for a subset
80% fail
GetInvalidPathChars() does not include all illegal characters (e.g., ':' is valid for drive letters but not in middle of path), leading to incomplete validation.
-
Truncating the path to a fixed length
90% fail
Truncation does not remove illegal characters; it may cut off valid parts and still leave invalid chars.