我想要做的是从文件夹中的每个文件及其下方的所有文件夹中获取6个文件信息。这些作品是:
我有以下代码,它会吐出一个带路径的文件列表,但是我试图找出如何在输出中获取其他信息。如果它可以用分号或其他东西分隔,那将是好的,因为最终的愿望是将此信息写入表格。任何帮助表示赞赏。
private void GetMyFiles () {
string rootDirectory = @"" + txtPathToScan.Text + "";
string extension = "*";
List<string> files = GetFilesRecursively (rootDirectory, extension);
Console.WriteLine ("Got {0} files of extension {1} in directory {2}", files.Count, extension, rootDirectory);
foreach (string file in files) {
Console.WriteLine (file);
}
}
/// <summary>
/// This function calls itself recursively for each of the subdirectories that it finds
/// in the root directory passed to it. It returns files of the extension as specified
/// by the caller.
/// </summary>
/// <param name="rootDirectory">The directory from which the file list is sought.</param>
/// <param name="extension">The particular extension for which the file list is sought.</param>
/// <returns>A list of all the files with extension as specified by the caller. This list
/// includes the files in the current directory as well its sub-directories.</returns>
static List<string> GetFilesRecursively (string rootDirectory, string extension) {
// Uncomment this line only if you want to trace the control as it goes from
// sub-directory to sub-directory. Be ready for long scrolls of text output:
//Console.WriteLine ("Currently in directory {0}", rootDirectory);
// Create an output list:
List<string> opList = new List<string> ();
// Get all files in the current directory:
string[] allFiles = Directory.GetFiles (rootDirectory, "*." + extension);
// Add these files to the output list:
opList.AddRange (allFiles);
// Get all sub-directories in current directory:
string[] subDirectories = Directory.GetDirectories (rootDirectory);
// And iterate through them:
foreach (string subDir in subDirectories) {
// Get all the files from the sub-directory:
List<string> subDirFileList = GetFilesRecursively (subDir, extension);
// And add it to this list:
opList.AddRange (subDirFileList);
}
// Finally return the output list:
return opList;
}
答案 0 :(得分:1)
你可以使用FileInfo
类......
var info = new System.IO.FileInfo(@"c:\folder\file.ext");
它具有Length
,CreationTime
,LastAccessTime
和LastWriteTime
的属性,可以将它们(例如)输出到控制台,您可以...
Console.WriteLine("For file {0}", info.Name);
Console.WriteLine("Length is {0}", info.Length);
Console.WriteLine("Creation time is {0}", info.CreationTime);
Console.WriteLine("Last access time is {0}", info.LastAccessTime);
Console.WriteLine("Last write time is {0}", info.LastWriteTime);
要获取完整文件名,您可以使用GetFileNameWithoutExtension
...
var fileName = System.IO.Path.GetFileNameWithoutExtension(@"c:\folder\file.ext")
在上面的示例中, filename
将等于"file"
。
获取文件扩展名的说明要多一些,但可以使用SHGetFIleInfo
完成,有关详细信息,请参阅this answer。
要将其与您当前的代码集成(如果您不那么担心文件说明),可以更改GetFilesRecursively
方法以返回List<FileInfo>
...
static List<FileInfo> GetFilesRecursively (string rootDirectory, string extension)
相应更新opList
变量...
List<FileInfo> opList = new List<FileInfo> ();
然后调整AddRange
来电添加FileInfo
个对象......
opList.AddRange (allFiles.Select(f => new FileInfo(f)));
最后,subDirFileList
的类型也需要更改,因此List<FileInfo>
。
(我认为这就是一切!)
答案 1 :(得分:0)
对于后人,完成的答案如下。如果您要投票,请仅投票给理查德的答案。谢谢!
private void GetMyFiles () {
string rootDirectory = @"" + txtPathToScan.Text + "";
string extension = "*";
List<FileInfo> files = GetFilesRecursively (rootDirectory, extension);
Console.WriteLine ("Got {0} files of extension {1} in directory {2}", files.Count, extension, rootDirectory);
foreach (FileInfo file in files)
{
Console.WriteLine(file);
var info = new System.IO.FileInfo(@"" + file + "");
Console.WriteLine("For file {0}", info.Name);
Console.WriteLine("Length is {0} KB", info.Length/1024);
Console.WriteLine("Creation time is {0}", info.CreationTime);
Console.WriteLine("Last access time is {0}", info.LastAccessTime);
Console.WriteLine("Last write time is {0}", info.LastWriteTime);
}
}
/// <summary>
/// This function calls itself recursively for each of the subdirectories that it finds
/// in the root directory passed to it. It returns files of the extension as specified
/// by the caller.
/// </summary>
/// <param name="rootDirectory">The directory from which the file list is sought.</param>
/// <param name="extension">The particular extension for which the file list is sought.</param>
/// <returns>A list of all the files with extension as specified by the caller. This list
/// includes the files in the current directory as well its sub-directories.</returns>
static List<FileInfo> GetFilesRecursively(string rootDirectory, string extension)
{
// Uncomment this line only if you want to trace the control as it goes from
// sub-directory to sub-directory. Be ready for long scrolls of text output:
//Console.WriteLine ("Currently in directory {0}", rootDirectory);
// Create an output list:
List<FileInfo> opList = new List<FileInfo>();
// Get all files in the current directory:
string[] allFiles = Directory.GetFiles (rootDirectory, "*." + extension);
// Add these files to the output list:
opList.AddRange(allFiles.Select(f => new FileInfo(f)));
// Get all sub-directories in current directory:
string[] subDirectories = Directory.GetDirectories (rootDirectory);
// And iterate through them:
foreach (string subDir in subDirectories) {
// Get all the files from the sub-directory:
List<FileInfo> subDirFileList = GetFilesRecursively (subDir, extension);
// And add it to this list:
opList.AddRange (subDirFileList);
}
// Finally return the output list:
return opList;
}