Need help in File handling with directory
-
Hi All, Can any one give me the suitable functions for the following in c# : 1. To find first instance of a file name that matches the file specification like *.jpg? like _tfindfirst( ) function in vc++ 2.To find the next name, if any, that matches the file specification? like _tfindfirst( ) function in VC++ 3. To find the Sub Directory? like _A_SUBDIR( ) function in vc++ 4. To ignore directory with . and .. ? Thanks
-
Hi All, Can any one give me the suitable functions for the following in c# : 1. To find first instance of a file name that matches the file specification like *.jpg? like _tfindfirst( ) function in vc++ 2.To find the next name, if any, that matches the file specification? like _tfindfirst( ) function in VC++ 3. To find the Sub Directory? like _A_SUBDIR( ) function in vc++ 4. To ignore directory with . and .. ? Thanks
Fore more file specification, please read FileInfo Help. The subdirectory . and .. are ignored.
static FileInfo FindFile(string startDirectory, string extension)
{
DirectoryInfo di = new DirectoryInfo(startDirectory);
foreach(FileInfo fi in di.GetFiles())
{
if(fi.Extension.ToLower() == extension)
return fi;
}
return null;
}static DirectoryInfo FindDirectory(string startDirectory, string dirNameToSearch)
{
DirectoryInfo di = new DirectoryInfo(startDirectory);
foreach(DirectoryInfo diSearch in di.GetDirectories())
{
if(diSearch.Name == dirNameToSearch)
return diSearch;
}
return null;
} -
Fore more file specification, please read FileInfo Help. The subdirectory . and .. are ignored.
static FileInfo FindFile(string startDirectory, string extension)
{
DirectoryInfo di = new DirectoryInfo(startDirectory);
foreach(FileInfo fi in di.GetFiles())
{
if(fi.Extension.ToLower() == extension)
return fi;
}
return null;
}static DirectoryInfo FindDirectory(string startDirectory, string dirNameToSearch)
{
DirectoryInfo di = new DirectoryInfo(startDirectory);
foreach(DirectoryInfo diSearch in di.GetDirectories())
{
if(diSearch.Name == dirNameToSearch)
return diSearch;
}
return null;
}Well done, you just did this guys homework! Current blacklist svmilky - Extremely rude | FeRtoll - Rude personal emails | ironstrike1 - Rude & Obnoxious behaviour