【问题标题】:Getting full path of file by name of file it gives access denied通过文件名获取文件的完整路径它会拒绝访问
【发布时间】:2015-09-18 05:11:54
【问题描述】:

我有一种情况,用户将在运行时输入文件名(不指定路径)。而且我必须通过c#代码找出文件。

我看到了一个函数GetFullPath(),但它只是给出了当前目录路径,附加了用户在运行时输入的文件名。

string fullPath;
Console.WriteLine("please enter teh name of the file to be searched");
String fileName = Console.ReadLine();
fullPath = Path.GetFullPath(fileName);

c# 中是否存在这样的方法来获取在运行时指定的文件的完整路径? (没有指定路径)。我可以说服用户指定驱动器(C:/D:/E:...),但在运行时编写完整路径以查找该文件,他们不会同意。

编辑:我的尝试是这样的:(但它拒绝访问)如果我不够聪明,无法访问每个目录,请帮助我,并且在我得到我的文件之前不要尝试打开安全文件夹。

 public static string Search(string fileName)
        {
            string fullPath = string.Empty;
            WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();
            WindowsPrincipal currentPrincipal = new WindowsPrincipal(currentIdentity);
            if (currentPrincipal.IsInRole(WindowsBuiltInRole.Administrator))
            {
                try
                {
                    foreach (string fpath in Directory.GetFiles("F:\\", "*", SearchOption.AllDirectories))
                    {
                        try
                        {
                            if (fpath.Substring(fpath.LastIndexOf("\\") + 1).ToUpper().Contains(fileName.ToUpper()))
                                fullPath = fpath;
                        }
                        catch (UnauthorizedAccessException)
                        {

                            Console.WriteLine("Access denied to folder1: " + fullPath);
                        }
                    }
                }
                catch (UnauthorizedAccessException)
                {

                    Console.WriteLine("Access denied to folder2: " + fullPath);
                }
            }
            else
            {
                Console.WriteLine("You are not authorized");
            }

            return fullPath;
        }

【问题讨论】:

  • 在这种情况下,您必须在用户提供的驱动器的所有目录中查找文件
  • 推荐的方法是使用文件选择器或拖放那里可以获得完整的文件路径
  • 是的,有(参见“重复”问题)。但根据用户的硬盘大小/速度,这将是一种令人难以置信的烦人体验。

标签: c# .net file c#-4.0 path


【解决方案1】:

如果您正在搜索文件,您可以使用以下命令搜索所有目录。假设用户输入整个文件名(包括扩展名)和源驱动器/位置。

string fullPath = string.Empty;
Console.WriteLine("please enter the name of the file to be searched");
String fileName = Console.ReadLine();

foreach(string fpath in Directory.GetFiles("C:\\", "*", SearchOption.AllDirectories))
{
    if (fpath.Substring(fpath.LastIndexOf("\\") + 1).ToUpper() == fileName.ToUpper()) 
                    fullpath = fpath;
}

或者,如果用户输入文件的一部分(不包括扩展名),请使用..

foreach(string fpath in Directory.GetFiles("C:\\", "*", SearchOption.AllDirectories))
{
    if (fpath.Substring(fpath.LastIndexOf("\\") + 1).ToUpper().Contains(fileName.ToUpper()))
                    fullpath = fpath;
}

如果找到多个结果(路径),则添加到数组或列表中。

像这样..

 var foundPaths = Directory.GetFiles("C:\\", "*", SearchOption.AllDirectories)
                .Where(x => x.ToUpper().Contains(fileName.ToUpper()))
                .Select(x => x)
                .ToList();

【讨论】:

  • 拒绝访问路径“C:\System Volume Information”。这种情况该怎么办?
【解决方案2】:

我自己找到了解决方案,我正在进行递归调用,直到我没有得到要搜索的文件:

 List<string> directories = new List<string>(Directory.GetDirectories(driveName));
                    string name=null;

                    foreach(string directry in directories)
                    {
                        if (GetFileInformation(directry, out name))
                        {
                            try
                            {
                                DirSearch(directry, fileName, ref  foundVar);         
                            }
                            catch (System.Exception excpt)
                            {
                                Console.WriteLine("from except msg :" + excpt.Message);
                                if(foundVar==true)
                                {                                    
                                    break;
                                }
                            }
                        }
                    }  

然后函数定义为:

   public static void DirSearch(string sDir, string fileName, ref bool foundVar)
    {
        try
        {
            foreach (string d in Directory.GetDirectories(sDir))
            {
                foreach (string f in Directory.GetFiles(d, fileName))
                {
                    if (Path.GetFileName(f) == fileName)
                    {
                        Console.WriteLine("directory is  and inside it is " + f);
                        OpenExeFile(f);
                        foundVar = true;
                        break;
                    }
                }
                DirSearch(d, fileName, ref foundVar);
            }
        }
        catch (System.Exception excpt)
        {
            Console.WriteLine(excpt.Message);
        }
    }

【讨论】:

    猜你喜欢
    • 2014-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-10
    • 2011-04-13
    • 1970-01-01
    • 2016-12-16
    • 1970-01-01
    相关资源
    最近更新 更多