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