如何获取给定文件的完整路径?
例如,我提供:
string filename = @"test.txt";
结果应该是:
Full File Path = C:\Windows\ABC\Test\test.txt
尝试
string fileName = "test.txt";
FileInfo f = new FileInfo(fileName);
string fullname = f.FullName;
您可以使用以下方法:
string path = Path.GetFullPath(FileName);
string dirpath = Directory.GetCurrentDirectory();
在文件名前添加此目录路径以获取完整路径。
正如@Dan Puzey在评论中指出的那样,最好使用Path.Combine。
Path.Combine(Directory.GetCurrentDirectory(), filename)
Path.Combine
,而不是在前面添加。 - Dan PuzeyVoid Main()
{
string filename = @"test.txt";
string filePath= AppDomain.CurrentDomain.BaseDirectory + filename ;
Console.WriteLine(filePath);
}
尝试..
Server.MapPath(FileUpload1.FileName);
MapPath
确实是正确的方法。你可以在这个链接上找到更多关于MapPath
的信息:http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath.aspx - JP HellemonsHttpContext
在 WinForms 中不可用。 - Abdul Munim尝试:
string fileName = @"test.txt";
string currentDirectory = Directory.GetCurrentDirectory();
string[] fullFilePath = Directory.GetFiles(currentDirectory, filename, SearchOption.AllDirectories);
它将返回当前目录及其子目录中所有这些文件的完整路径到字符串数组fullFilePath。如果只有一个文件存在,则该文件将在“fullFileName [0]”中。
private const string BulkSetPriceFile = "test.txt";
...
var fullname = Path.GetFullPath(BulkSetPriceFile);
您可以获取当前路径:
string AssemblyPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location).ToString();
祝你好运!
test.txt
文件并返回其路径,还是你想将一些默认路径附加到该文件? - Habib