0

I have a situation where the user will enter the name of the file and at run-time (without specifying the path). And i must find out the file by c# code.

I have seen a function GetFullPath() but it just give the current directory path appended by fileName entered by user at run-time.

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

Is there any such way exist in c# to get full path of a file specified at run time ? (Without specifying about the path). I can convince the user for specifying the Drive (C:/D:/E:...) but for writing the full path at run time to find that file they will not agree.

EDIT: My try is this: (but it gives access denied) please help me if i am not smart enough to go to each directory and do not try to open the secured folder until i get my file.

 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;
        }
xav xav
  • 231
  • 2
  • 5
  • 12
  • 2
    in that case you will have to find file in all directories in the drive user gave – Ehsan Sajjad Sep 18 '15 at 05:16
  • 1
    the recommended way is to use a filepicker or drag drop there you can get the full filepath – soumya sambit Kunda Sep 18 '15 at 05:19
  • 2
    possible duplicate of [How to search all directories in all drives for .txt files?](http://stackoverflow.com/questions/17613155/how-to-search-all-directories-in-all-drives-for-txt-files) – Kamil Budziewski Sep 18 '15 at 05:23
  • Yes, there is (see "duplicate" questions). But depending on the users hard drive size / speed it will be an incredibly annoying experience. – Christian.K Sep 18 '15 at 05:54

2 Answers2

1

If you're searching for a file you can use the following to search all directories. Assuming the user inputs the entire filename (including the extension) and a source drive/location.

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

Alternatively, if the user inputs part of the file (excluding extention) use..

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

Add to an array or list incase multiple results (paths) are found.

Like so..

 var foundPaths = Directory.GetFiles("C:\\", "*", SearchOption.AllDirectories)
                .Where(x => x.ToUpper().Contains(fileName.ToUpper()))
                .Select(x => x)
                .ToList();
Magic Mick
  • 1,475
  • 1
  • 21
  • 32
-1

I found solution my self, I am doing recursive call until i don't get file to be searched:

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

And then the function definition is :

   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);
        }
    }
xav xav
  • 231
  • 2
  • 5
  • 12