C# 如何从文件名中获取完整的文件路径?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13175868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 07:42:02  来源:igfitidea点击:

How to get full file path from file name?

c#winforms

提问by Ranjeeth Kumar Pathi

How do I get the full path for a given file?

如何获取给定文件的完整路径?

e.g. I provide:

例如我提供:

string filename = @"test.txt";

Result should be:

结果应该是:

Full File Path = C:\Windows\ABC\Test\test.txt

回答by rohan panchal

try..

尝试..

Server.MapPath(FileUpload1.FileName);

回答by Anirudh Ramanathan

Directory.GetCurrentDirectory

Directory.GetCurrentDirectory

string dirpath = Directory.GetCurrentDirectory();

Prependthis dirpath to the filename to get the complete path.

前面加上这个dirpath到文件名,以获得完整路径。

As @Dan Puzey indicated in the comments, it would be better to use Path.Combine

正如@Dan Puzey 在评论中指出的那样,最好使用Path.Combine

Path.Combine(Directory.GetCurrentDirectory(), filename)

回答by codingbiz

Try

尝试

string fileName = "test.txt";
FileInfo f = new FileInfo(fileName);
string fullname = f.FullName;

回答by Matthew Watson

Use Path.GetFullPath():

使用 Path.GetFullPath():

http://msdn.microsoft.com/en-us/library/system.io.path.getfullpath.aspx

http://msdn.microsoft.com/en-us/library/system.io.path.getfullpath.aspx

This should return the full path information.

这应该返回完整的路径信息。

回答by Yaugen Vlasau

private const string BulkSetPriceFile = "test.txt";
...
var fullname = Path.GetFullPath(BulkSetPriceFile);

回答by sanjoy

You Can use:

您可以使用:

string path = Path.GetFullPath(FileName);

it will return the Full path of that mentioned file.

它将返回该文件的完整路径。

回答by Akhil Achuthan

try:

尝试:

string fileName = @"test.txt";
    string currentDirectory = Directory.GetCurrentDirectory();
    string[] fullFilePath = Directory.GetFiles(currentDirectory, filename, SearchOption.AllDirectories);

it will return full path of all such files in the current directory and its sub directories to string array fullFilePath. If only one file exist it will be in "fullFileName[0]".

它会将当前目录及其子目录中所有此类文件的完整路径返回到字符串数组 fullFilePath。如果只有一个文件存在,它将在“fullFileName[0]”中。

回答by Aishu

I know my answer it's too late, but it might helpful to other's
Try,

我知道我的答案为时已晚,但它可能对其他人的
尝试有所帮助,

Void Main()
{
string filename = @"test.txt";
string filePath= AppDomain.CurrentDomain.BaseDirectory + filename ;
Console.WriteLine(filePath);
}

回答by David Castro

You can get the current path:

您可以获取当前路径:

string AssemblyPath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location).ToString();

Good luck!

祝你好运!