【问题标题】:Get Absolute Path of file in C# from file name [duplicate]从文件名获取C#中文件的绝对路径[重复]
【发布时间】:2020-04-10 20:03:37
【问题描述】:

我的桌面上有一个文件,我想在我的代码中获取文件的完整路径,它应该在我的桌面上还是任何地方

我的代码是这样的

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GetFullPath
{
    class Program
    {
        static void Main(string[] args)
        {
            string filename = "eMemoExpenseApproval.docx";

            string fullFilePath = Path.Combine(Directory.GetCurrentDirectory(), filename);
            Console.Write("Path : " + fullFilePath);
            Console.Read();
        }
    }
}

而不是从桌面获取完整路径,它显示了来自 Visual Studio 的路径,这不应该是这样,但我得到了这个

Path : C:\Users\Administrator\Documents\Visual Studio 2017\Projects\GetFullPath\GetFullPath\bin\Debug\eMemoExpenseApproval.docx

编辑:

这可以获取桌面上文件的路径

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GetFullPath
{
    class Program
    {
        static void Main(string[] args)
        {
            string filename = "eMemoExpenseApproval.docx";

            string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            string fullFilePath = path +"/"+ filename;
            Console.Write("Path : " + fullFilePath);
            Console.Read();
        }
    }
}

很好,但是其他目录呢?

【问题讨论】:

  • Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
  • 一般来说,当您的进程开始时,您不应该对Directory.GetCurrentDirectory() 指向的位置做出任何假设。
  • 如上述 cmets 所指。阅读此docs.microsoft.com/en-us/dotnet/api/…
  • 问题是:为什么你认为GetCurrentDirectory是桌面的路径?
  • 不清楚你在问什么。您的意思是在整个磁盘中搜索该文件名吗?在这种情况下,您的代码完全关闭。

标签: c#


【解决方案1】:

Directory.GetCurrentDirectory() 实际上返回应用程序执行的目录。 如果您知道该文件位于桌面中,则可以改为执行以下操作: string fullFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop, filename));

【讨论】:

  • 这是不正确的,你应该使用Environment.GetFolderPath(Environment.SpecialFolder.Desktop);上面的每个cmets
  • @PavelAnikhouski,其他目录怎么样?
  • @Hamish 你指的还有哪些其他目录?
  • @PavelAnikhouski,我怎样才能得到这样的绝对路径,例如 stackoverflow.com/questions/51520/… 只是这一次,它的 c#
  • @Hamish Path.GetFullPath 代表that
【解决方案2】:

据我了解,您想在一组有限的文件夹中搜索命名文件。为此,请声明如下函数:

IEnumerable<string> FindInMultipleFolders(string[] folders, string filename)
{   
    var result = new List<string>();
    foreach (var folder in folders)
    {
        var dirs = Directory.GetFiles(folder, filename);
        foreach (String dir in dirs) 
        {
            result.Add(dir);
        }
    }
    return result;
}

然后用文件名和要搜索的文件夹来调用它,如下所示:

FindInMultipleFolders(
    new string[] 
    {
        Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
        Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
        @"C:\Some\Other\Folder\I\Would\Like\Searched"
    }, 
    "eMemoExpenseApproval.docx");
}

该文件可能位于多个文件夹中,因此该函数返回一个IEnumerable&lt;string&gt;FindInMultipleFolders 只搜索传递的文件夹,不搜索子文件夹。如果要搜索子文件夹,应将SearchOption.AllDirectories 作为第三个参数添加到GetFiles。然后你可以搜索整个硬盘驱动器:

FindInMultipleFolders(
    new string[] 
    {
        @"C:\"
    }, 
    "eMemoExpenseApproval.docx");
}

【讨论】:

    猜你喜欢
    • 2012-12-23
    • 2013-11-17
    • 2014-04-08
    • 2013-02-06
    • 2011-11-22
    • 1970-01-01
    • 2012-10-04
    相关资源
    最近更新 更多