有没有办法验证有效的路径/文件名?

Is there a way to verify a valid path / file name?

有没有办法验证有效的路径/文件名?文件名已知,因为它已提供给客户。 .exe 和其他文件的放置位置未知。

要检查指定路径下是否存在文件,请使用System.IO.File.Exists(string path)

if (File.Exists(pathFileName))
{
    ...
}
else
{
    ...
}

要检查路径或文件名是否有效(即不包含非法字符)使用 System.IO.Path.GetInvalidPathChars()System.IO.Path.GetInvalidFileNameChars():

if (Path.GetInvalidFileNameChars().Any(c => pathFileName.Contains(c)))
{
    ...
}

由于您尝试验证路径是否确实存在,因此您应该能够使用 System.IO

var path = "...";
if(Directory.Exist(path))
{
     // Valid / Exist
}

您也可以验证文件,只需使用 File.Exist。可以查到资料here.

The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. Trailing spaces are removed from the end of the path parameter before checking whether the directory exists. The path parameter is not case-sensitive. If you do not have at a minimum read-only permission to the directory, the Exists method will return false. The Exists method returns false if any error occurs while trying to determine if the specified file exists. This can occur in situations that raise exceptions such as passing a file name with invalid characters or too many characters, a failing or missing disk, or if the caller does not have permission to read the file.