主要代码:
static void Main(string[] args)
{
MySqlConnection mcon = new MySqlConnection("connection credentials...");
MySqlDataReader myreader = null;
MySqlCommand cmd = new MySqlCommand("select * from files", mcon);
mcon.Open();
myreader = cmd.ExecuteReader();
//List of path's from MySQL table
List<String> list = new List<String>();
while (myreader.Read())
{
//Appending the list of all path names from MySQL table
list.Add(myreader[1].ToString());
//Retrieving id number of path from MySQL table (see list before 'private static void directoryChange')
list2.Add(myreader.GetInt32(0));
}
mcon.Close();
//This watches all path's listed in MySQL table
foreach (string i in list)
{
Console.WriteLine(i);
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = Path.GetDirectoryName($@"{i}"); //get specific files listed in MySQL table
watcher.Filter = Path.GetFileName($@"{i}");
watcher.EnableRaisingEvents = true;
watcher.IncludeSubdirectories = true;
watcher.Created += directoryChange;
watcher.Deleted += directoryChange;
watcher.Renamed += onRename;
}
Console.Read();
}
//List of Id numbers from MySQL table
static List<int> list2 = new List<int>();
private static void directoryChange(object source, System.IO.FileSystemEventArgs e)
{
if (e.ChangeType == System.IO.WatcherChangeTypes.Deleted){
Console.WriteLine($"Deleted {e.FullPath}"); //Here I need to get the id number of file that has been deleted
}
}
目标:
我的目标是从MySQL获取已删除文件的id
号。
更新:
这是我到现在为止的位置:
static void Main(string[] args)
{
MySqlConnection mcon = new MySqlConnection("connection credentials...");
MySqlDataReader myreader = null;
MySqlCommand cmd = new MySqlCommand("select * from files", mcon);
mcon.Open();
myreader = cmd.ExecuteReader();
//List of path's from MySQL table
List<String> list = new List<String>();
//List of Id numbers from MySQL table
static List<int> list2 = new List<int>();
while (myreader.Read())
{
//Appending the list of all path names from MySQL table
list.Add(myreader[1].ToString());
//Retrieving id number of path from MySQL table (see list before 'private static void directoryChange')
list2.Add(myreader.GetInt32(0));
}
mcon.Close();
//This watches all path's listed in MySQL table
var data = list.Zip(list2, (n, w) => new {Path= n, ID = w});
foreach(var nw in data)
{
int result = nw.ID;
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = Path.GetDirectoryName($@"{nw.Path}");
watcher.Filter = Path.GetFileName($@"{nw.Path}");
watcher.EnableRaisingEvents = true;
watcher.IncludeSubdirectories = true;
watcher.Created += directoryChange;
watcher.Deleted += directoryChange;
watcher.Renamed += onRename;
}
Console.Read();
}
//Tried passing over the id from for loop
static int result;
private static void directoryChange(object source, System.IO.FileSystemEventArgs e)
{
if (e.ChangeType == System.IO.WatcherChangeTypes.Deleted){
Console.WriteLine($"Deleted {e.FullPath}");
Console.WriteLine(result);
}
}
这不起作用,因为结果显示为0
。请问我是否可以解决此问题。
所以回顾一下我从您的代码中解析的内容:
因此,只需保留一个包含数据的字典,而不是两个列表或单个静态变量?
// Class-level field
private static fileList = new Dictionary<string, int>();
// Then while reading the data:
while (myreader.Read())
{
// Use path as a key to the id, so you can use the path later to look up the key
fileList[myreader[1].ToString()] = myreader.GetInt32(0);
}
foreach(var nw in fileList)
{
// ...
}
然后在您的事件处理程序中:
private static void directoryChange(object source, System.IO.FileSystemEventArgs e)
{
if (e.ChangeType == System.IO.WatcherChangeTypes.Deleted)
{
if (fileList.TryGetValue(e.FullPath, out var id))
{
Console.WriteLine(id);
}
else
{
Console.WriteLine($"Path '{e.FullPath}' not present in dictionary?");
}
}
}