我正在用C#编写一个小程序来扫描文件夹并打开在程序上按下按钮后的下午5点30分之后创建的文件.这也必须在子文件夹中搜索.
我需要一些解决方案来指出正确的方向,因为我不确定如何做到这一点.
这是文件夹观察程序的一部分.问题是当用户回家时,PC被关闭,并且在17.30之后有文件被创建到目录.所以我需要一种方法,当程序在早上重新启动时检测到17.30之后创建的任何内容并打开它们.
private void button1_Click(object sender, EventArgs e)
{
folderBrowser.ShowDialog();
textBox1.Text = folderBrowser.SelectedPath;
filewatcher.Path = textBox1.Text;
Registry.SetValue("HKEY_CURRENT_USER\\SOFTWARE\\COMPANY\\FOLDERWATCHER", "FOLDERPATH", textBox1.Text);
}
private void Form1_Load(object sender, EventArgs e)
{
String WatchFolder = Registry.GetValue("HKEY_CURRENT_USER\\SOFTWARE\\COMPANY\\FOLDERWATCHER", "FOLDERPATH", "").ToString();
textBox1.Text = WatchFolder;
filewatcher.Path = WatchFolder;
}
private void Form1_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
ShowInTaskbar = true;
Hide();
}
}
private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e)
{
if(!e.FullPath.EndsWith("temp.temp"))
{
MessageBox.Show("You have a Collection Form: " + e.Name);
Process.Start("explorer.exe", e.FullPath);
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
Show();
}
}
Run Code Online (Sandbox Code Playgroud)
这是我上面的完整代码.我想用一个按钮打开或显示17.30之后创建的文件.
Nei*_*l N 19
查看System.IO命名空间,它拥有您需要的一切.
在DirectoryInfo中与文件类会做你想要什么.
这是您正在寻找的递归方法:
public static List<string> GetFilesCreatedAfter(string directoryName, DateTime dt)
{
var directory = new DirectoryInfo(directoryName);
if (!directory.Exists)
throw new InvalidOperationException("Directory does not exist : " + directoryName);
var files = new List<string>();
files.AddRange(directory.GetFiles().Where(n => n.CreationTime > dt).Select(n=>n.FullName));
foreach (var subDirectory in Directory.GetDirectories(directoryName))
{
files.AddRange(GetFilesCreatedAfter(subDirectory,dt));
}
return files;
}
Run Code Online (Sandbox Code Playgroud)
希望我帮忙.