private void btnSearch_Click(object sender, EventArgs e) { List <FileList> oLst = new List <FileList>(); string partialName = "webapi"; DirectoryInfo hdDirectoryInWhichToSearch = new DirectoryInfo(@"C:\MyFolder"); FileSystemInfo[] filesAndDirs = hdDirectoryInWhichToSearch.GetFileSystemInfos("*" + partialName + "*"); foreach (FileSystemInfo foundFile in filesAndDirs) { string fullName = foundFile.FullName; Console.WriteLine(fullName); if (foundFile.GetType() == typeof(FileInfo)) { FileInfo fileInfo = (FileInfo)foundFile; oLst.Add(new FileList { Name = fileInfo.Name, Type = "File", location = fileInfo.FullName, Size = Format.ByteSize(fileInfo.Length) }); } if (foundFile.GetType() == typeof(DirectoryInfo)) { Console.WriteLine("... is a directory"); DirectoryInfo directoryInfo = (DirectoryInfo)foundFile; FileInfo[] subfileInfos = directoryInfo.GetFiles(); oLst.Add(new FileList { Name = directoryInfo.Name, Type = "Folder", location = directoryInfo.FullName, Size = Format.ByteSize(GetDirectorySize(directoryInfo.FullName)) }); } } dataGridView1.DataSource = oLst; }
public string Format_byte_size(long size) { return(Format.ByteSize(size)); }