你为什么不使用 File 类?永远使用它!
File f = null;
string sPathToTest = "C:\Test.txt";
try{
f = new File(sPathToTest );
}catch(Exception e){
Console.WriteLine(string.Format("The file \"{0}\" is not a valid path, Error : {1}.", sPathToTest , e.Message);
}
MSDN:http: //msdn.microsoft.com/en-gb/library/system.io.file%28v=vs.80%29.aspx
也许您只是在寻找 File.Exists ( http://msdn.microsoft.com/en-gb/library/system.io.file.exists%28v=vs.80%29.aspx )
还要看看 Path 类(http://msdn.microsoft.com/en-us/library/system.io.path.aspx)
GetAbsolutePath 可能是获得您想要的东西的一种方法!(http://msdn.microsoft.com/en-us/library/system.io.path.getfullpath.aspx)
string sPathToTest = "C:\Test.txt";
string sAbsolutePath = "";
try{
sAbsolutePath = Path.GetAbsolutePath(sPathToTest);
if(!string.IsNullOrEmpty(sAbsolutePath)){
Console.WriteLine("Path valid");
}else{
Console.WriteLine("Bad path");
}
}catch(Exception e){
Console.WriteLine(string.Format("The file \"{0}\" is not a valid path, Error : {1}.", sPathToTest , e.Message);
}