FileInfo path with '|' gives illegal characters in path exception?
When I try to construct a FileInfo object using a string that contains a '|' character, I'm getting an Illegal characters in path exception. The string is just parsed from some data source I don't have control over, so I can't add any escape characters to the original string. So I tried a .Replace("|","|") on the string but still get the same exception. What's the proper way to escape this so I don't get the exception?
Also, is there a way to get the offending character in the exception so my exception handling can correct it automatically and I don't need to wait for the next magic character that isn't allowed to start crashing everything before I can handle the error?
c# exception file-io
add a comment |
When I try to construct a FileInfo object using a string that contains a '|' character, I'm getting an Illegal characters in path exception. The string is just parsed from some data source I don't have control over, so I can't add any escape characters to the original string. So I tried a .Replace("|","|") on the string but still get the same exception. What's the proper way to escape this so I don't get the exception?
Also, is there a way to get the offending character in the exception so my exception handling can correct it automatically and I don't need to wait for the next magic character that isn't allowed to start crashing everything before I can handle the error?
c# exception file-io
2
You should provide a code sample that demonstrates the problem you're having. But the issue here is that the pipe character (|
) is invalid for directory and file names, so you'd have to strip it out completely. The string you're getting from the data source is not a valid path.
– Rufus L
Nov 16 '18 at 0:36
Check against GetInvalidPathChars: docs.microsoft.com/en-us/dotnet/api/…
– Camilo Terevinto
Nov 16 '18 at 0:41
See Windows' reserved characters (which you can get via the C# method Camilo mentioned)
– John
Nov 16 '18 at 0:54
1
filePath = Path.GetInvalidPathChars().Aggregate(filePath, (fPath, chr) => fPath.Replace(chr.ToString(), ""));
...will remove all the invalid path characters from a string namedfilePath
. Note that there are additional characters that are invalid for a file name that are valid for a path, like: * ? /
.
– Rufus L
Nov 16 '18 at 1:07
1
Path.GetInvalidPathChars().ToList().ForEach(c => filePath = filePath.Replace(c.ToString(), ""));
is another linq-y way to do it.
– Rufus L
Nov 16 '18 at 1:12
add a comment |
When I try to construct a FileInfo object using a string that contains a '|' character, I'm getting an Illegal characters in path exception. The string is just parsed from some data source I don't have control over, so I can't add any escape characters to the original string. So I tried a .Replace("|","|") on the string but still get the same exception. What's the proper way to escape this so I don't get the exception?
Also, is there a way to get the offending character in the exception so my exception handling can correct it automatically and I don't need to wait for the next magic character that isn't allowed to start crashing everything before I can handle the error?
c# exception file-io
When I try to construct a FileInfo object using a string that contains a '|' character, I'm getting an Illegal characters in path exception. The string is just parsed from some data source I don't have control over, so I can't add any escape characters to the original string. So I tried a .Replace("|","|") on the string but still get the same exception. What's the proper way to escape this so I don't get the exception?
Also, is there a way to get the offending character in the exception so my exception handling can correct it automatically and I don't need to wait for the next magic character that isn't allowed to start crashing everything before I can handle the error?
c# exception file-io
c# exception file-io
edited Nov 16 '18 at 0:55
John
13.3k32544
13.3k32544
asked Nov 16 '18 at 0:33
ThundercleezThundercleez
7029
7029
2
You should provide a code sample that demonstrates the problem you're having. But the issue here is that the pipe character (|
) is invalid for directory and file names, so you'd have to strip it out completely. The string you're getting from the data source is not a valid path.
– Rufus L
Nov 16 '18 at 0:36
Check against GetInvalidPathChars: docs.microsoft.com/en-us/dotnet/api/…
– Camilo Terevinto
Nov 16 '18 at 0:41
See Windows' reserved characters (which you can get via the C# method Camilo mentioned)
– John
Nov 16 '18 at 0:54
1
filePath = Path.GetInvalidPathChars().Aggregate(filePath, (fPath, chr) => fPath.Replace(chr.ToString(), ""));
...will remove all the invalid path characters from a string namedfilePath
. Note that there are additional characters that are invalid for a file name that are valid for a path, like: * ? /
.
– Rufus L
Nov 16 '18 at 1:07
1
Path.GetInvalidPathChars().ToList().ForEach(c => filePath = filePath.Replace(c.ToString(), ""));
is another linq-y way to do it.
– Rufus L
Nov 16 '18 at 1:12
add a comment |
2
You should provide a code sample that demonstrates the problem you're having. But the issue here is that the pipe character (|
) is invalid for directory and file names, so you'd have to strip it out completely. The string you're getting from the data source is not a valid path.
– Rufus L
Nov 16 '18 at 0:36
Check against GetInvalidPathChars: docs.microsoft.com/en-us/dotnet/api/…
– Camilo Terevinto
Nov 16 '18 at 0:41
See Windows' reserved characters (which you can get via the C# method Camilo mentioned)
– John
Nov 16 '18 at 0:54
1
filePath = Path.GetInvalidPathChars().Aggregate(filePath, (fPath, chr) => fPath.Replace(chr.ToString(), ""));
...will remove all the invalid path characters from a string namedfilePath
. Note that there are additional characters that are invalid for a file name that are valid for a path, like: * ? /
.
– Rufus L
Nov 16 '18 at 1:07
1
Path.GetInvalidPathChars().ToList().ForEach(c => filePath = filePath.Replace(c.ToString(), ""));
is another linq-y way to do it.
– Rufus L
Nov 16 '18 at 1:12
2
2
You should provide a code sample that demonstrates the problem you're having. But the issue here is that the pipe character (
|
) is invalid for directory and file names, so you'd have to strip it out completely. The string you're getting from the data source is not a valid path.– Rufus L
Nov 16 '18 at 0:36
You should provide a code sample that demonstrates the problem you're having. But the issue here is that the pipe character (
|
) is invalid for directory and file names, so you'd have to strip it out completely. The string you're getting from the data source is not a valid path.– Rufus L
Nov 16 '18 at 0:36
Check against GetInvalidPathChars: docs.microsoft.com/en-us/dotnet/api/…
– Camilo Terevinto
Nov 16 '18 at 0:41
Check against GetInvalidPathChars: docs.microsoft.com/en-us/dotnet/api/…
– Camilo Terevinto
Nov 16 '18 at 0:41
See Windows' reserved characters (which you can get via the C# method Camilo mentioned)
– John
Nov 16 '18 at 0:54
See Windows' reserved characters (which you can get via the C# method Camilo mentioned)
– John
Nov 16 '18 at 0:54
1
1
filePath = Path.GetInvalidPathChars().Aggregate(filePath, (fPath, chr) => fPath.Replace(chr.ToString(), ""));
...will remove all the invalid path characters from a string named filePath
. Note that there are additional characters that are invalid for a file name that are valid for a path, like : * ? /
.– Rufus L
Nov 16 '18 at 1:07
filePath = Path.GetInvalidPathChars().Aggregate(filePath, (fPath, chr) => fPath.Replace(chr.ToString(), ""));
...will remove all the invalid path characters from a string named filePath
. Note that there are additional characters that are invalid for a file name that are valid for a path, like : * ? /
.– Rufus L
Nov 16 '18 at 1:07
1
1
Path.GetInvalidPathChars().ToList().ForEach(c => filePath = filePath.Replace(c.ToString(), ""));
is another linq-y way to do it.– Rufus L
Nov 16 '18 at 1:12
Path.GetInvalidPathChars().ToList().ForEach(c => filePath = filePath.Replace(c.ToString(), ""));
is another linq-y way to do it.– Rufus L
Nov 16 '18 at 1:12
add a comment |
2 Answers
2
active
oldest
votes
It doesn't sound like your data source is providing valid file paths/names for the operating system you're using.
You can use Path.GetInvalidFileNameChars()
and Path.GetInvalidPathChars()
to get arrays of characters that aren't allowed in a filename or path. These would need to be removed not escaped to be legal for a file system. Stripping these out is likely a work around, rather than a fix. It would be best to resolve the issue at the source of your data.
add a comment |
Here is a sample method that will remove invalid path characters from the path portion of the string, and invalid file characters from the file name portion of the input string:
public static string RemoveIllegalChars(string path)
{
if (string.IsNullOrWhiteSpace(path)) return path;
// Remove invalid directory characters
Path.GetInvalidPathChars().ToList()
.ForEach(c => path = path.Replace(c.ToString(), ""));
// Remove invalid file name characters from file name portion and return the result
return Path.Combine(Path.GetDirectoryName(path),
Path.GetInvalidFileNameChars()
.Aggregate(Path.GetFileName(path), (fileName, invalidChar) =>
fileName.Replace(invalidChar.ToString(), "")));
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329782%2ffileinfo-path-with-gives-illegal-characters-in-path-exception%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
It doesn't sound like your data source is providing valid file paths/names for the operating system you're using.
You can use Path.GetInvalidFileNameChars()
and Path.GetInvalidPathChars()
to get arrays of characters that aren't allowed in a filename or path. These would need to be removed not escaped to be legal for a file system. Stripping these out is likely a work around, rather than a fix. It would be best to resolve the issue at the source of your data.
add a comment |
It doesn't sound like your data source is providing valid file paths/names for the operating system you're using.
You can use Path.GetInvalidFileNameChars()
and Path.GetInvalidPathChars()
to get arrays of characters that aren't allowed in a filename or path. These would need to be removed not escaped to be legal for a file system. Stripping these out is likely a work around, rather than a fix. It would be best to resolve the issue at the source of your data.
add a comment |
It doesn't sound like your data source is providing valid file paths/names for the operating system you're using.
You can use Path.GetInvalidFileNameChars()
and Path.GetInvalidPathChars()
to get arrays of characters that aren't allowed in a filename or path. These would need to be removed not escaped to be legal for a file system. Stripping these out is likely a work around, rather than a fix. It would be best to resolve the issue at the source of your data.
It doesn't sound like your data source is providing valid file paths/names for the operating system you're using.
You can use Path.GetInvalidFileNameChars()
and Path.GetInvalidPathChars()
to get arrays of characters that aren't allowed in a filename or path. These would need to be removed not escaped to be legal for a file system. Stripping these out is likely a work around, rather than a fix. It would be best to resolve the issue at the source of your data.
answered Nov 16 '18 at 0:44
dariomdariom
3,9902440
3,9902440
add a comment |
add a comment |
Here is a sample method that will remove invalid path characters from the path portion of the string, and invalid file characters from the file name portion of the input string:
public static string RemoveIllegalChars(string path)
{
if (string.IsNullOrWhiteSpace(path)) return path;
// Remove invalid directory characters
Path.GetInvalidPathChars().ToList()
.ForEach(c => path = path.Replace(c.ToString(), ""));
// Remove invalid file name characters from file name portion and return the result
return Path.Combine(Path.GetDirectoryName(path),
Path.GetInvalidFileNameChars()
.Aggregate(Path.GetFileName(path), (fileName, invalidChar) =>
fileName.Replace(invalidChar.ToString(), "")));
}
add a comment |
Here is a sample method that will remove invalid path characters from the path portion of the string, and invalid file characters from the file name portion of the input string:
public static string RemoveIllegalChars(string path)
{
if (string.IsNullOrWhiteSpace(path)) return path;
// Remove invalid directory characters
Path.GetInvalidPathChars().ToList()
.ForEach(c => path = path.Replace(c.ToString(), ""));
// Remove invalid file name characters from file name portion and return the result
return Path.Combine(Path.GetDirectoryName(path),
Path.GetInvalidFileNameChars()
.Aggregate(Path.GetFileName(path), (fileName, invalidChar) =>
fileName.Replace(invalidChar.ToString(), "")));
}
add a comment |
Here is a sample method that will remove invalid path characters from the path portion of the string, and invalid file characters from the file name portion of the input string:
public static string RemoveIllegalChars(string path)
{
if (string.IsNullOrWhiteSpace(path)) return path;
// Remove invalid directory characters
Path.GetInvalidPathChars().ToList()
.ForEach(c => path = path.Replace(c.ToString(), ""));
// Remove invalid file name characters from file name portion and return the result
return Path.Combine(Path.GetDirectoryName(path),
Path.GetInvalidFileNameChars()
.Aggregate(Path.GetFileName(path), (fileName, invalidChar) =>
fileName.Replace(invalidChar.ToString(), "")));
}
Here is a sample method that will remove invalid path characters from the path portion of the string, and invalid file characters from the file name portion of the input string:
public static string RemoveIllegalChars(string path)
{
if (string.IsNullOrWhiteSpace(path)) return path;
// Remove invalid directory characters
Path.GetInvalidPathChars().ToList()
.ForEach(c => path = path.Replace(c.ToString(), ""));
// Remove invalid file name characters from file name portion and return the result
return Path.Combine(Path.GetDirectoryName(path),
Path.GetInvalidFileNameChars()
.Aggregate(Path.GetFileName(path), (fileName, invalidChar) =>
fileName.Replace(invalidChar.ToString(), "")));
}
answered Nov 16 '18 at 1:35
Rufus LRufus L
19.2k31732
19.2k31732
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53329782%2ffileinfo-path-with-gives-illegal-characters-in-path-exception%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
You should provide a code sample that demonstrates the problem you're having. But the issue here is that the pipe character (
|
) is invalid for directory and file names, so you'd have to strip it out completely. The string you're getting from the data source is not a valid path.– Rufus L
Nov 16 '18 at 0:36
Check against GetInvalidPathChars: docs.microsoft.com/en-us/dotnet/api/…
– Camilo Terevinto
Nov 16 '18 at 0:41
See Windows' reserved characters (which you can get via the C# method Camilo mentioned)
– John
Nov 16 '18 at 0:54
1
filePath = Path.GetInvalidPathChars().Aggregate(filePath, (fPath, chr) => fPath.Replace(chr.ToString(), ""));
...will remove all the invalid path characters from a string namedfilePath
. Note that there are additional characters that are invalid for a file name that are valid for a path, like: * ? /
.– Rufus L
Nov 16 '18 at 1:07
1
Path.GetInvalidPathChars().ToList().ForEach(c => filePath = filePath.Replace(c.ToString(), ""));
is another linq-y way to do it.– Rufus L
Nov 16 '18 at 1:12