簡體   English   中英

通過文件名獲取文件的完整路徑會拒絕訪問

[英]Getting full path of file by name of file it gives access denied

我遇到一種情況,用戶將在運行時輸入文件名(而未指定路徑)。 而且我必須通過C#代碼找出文件。

我已經看到了一個函數GetFullPath()但是它只給出了當前目錄路徑,該目錄路徑由用戶在運行時輸入的fileName附加。

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;
        }

如果要搜索文件,則可以使用以下內容搜索所有目錄。 假設用戶輸入了整個文件名(包括擴展名)和源驅動器/位置。

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();

我找到了解決方案,我正在進行遞歸調用,直到我沒有要搜索的文件為止:

 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);
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM