gpt4 book ai didi

c# - 如何在filewatcher中更新文件名

转载 作者:太空宇宙 更新时间:2023-11-03 15:51:17 26 4
gpt4 key购买 nike

最后,我编写了自动更新的代码。文件被添加到数据库中。但我无法更新第 0 列中的文件名。它只给出了第一次文件名。当我将文件名更改为其他名称时,它不会在 column0 中给出。

我只需要在部分中输入特定的文件名。我不知道如何修剪文件名并在 column0 中给出。请帮帮我。

此外,我发现了一个重要的错误。当我使用按钮将文件从单元格 [1] 移动到单元格 [2] 时。整个文件被移动到单元格 [2]。所以那个时候文件观察者正在寻找那个特定的文件夹并理解,文件被删除了..所以代码将删除数据库..我不想那样做..我该如何处理这种情况..

请帮帮我

我的自动更新代码片段:

 namespace shell_FileSystemWatcher
{
public partial class Form1 : Form
{
public string partKey = "";
public Form1()
{
InitializeComponent();
}

/// <summary>
/// Create a new object of FileSystemWatcher
/// </summary>
FileSystemWatcher watcher = new FileSystemWatcher();
/// <summary>
/// Initialize the
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
ListBox listbox1 = new ListBox();
private void Form1_Load_1(object sender, EventArgs e)
{
///Creating listbox in current form

this.Controls.Add(listbox1);
listbox1.Size = new Size(500, 200);
listbox1.Location = new Point(0, 0);
listbox1.Visible = true;

///Assigning some properties to FileSystemWatcher class object
///Assign the path
watcher.Path = @"C:\user\elec\copy";
///Assign the filters as your requirement
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;

///Handle the events that will be called when any file will be changed in the given folder path.
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);

///Enabling the event call
watcher.EnableRaisingEvents = true;

///Initializing delegate that we have created to update UI control outside the current thread
addItemInList = new delAddItem(this.AddString);
}

// Define the event handlers.
private void OnChanged(object source, FileSystemEventArgs e)
{
FileInfo file = new FileInfo(e.FullPath);
Console.WriteLine(file.Name);
switch (e.ChangeType)
{
case WatcherChangeTypes.Created:
//Insert file in database
this.Invoke(addItemInList, "File: " + e.FullPath + " Created");
{
SqlConnection con = new SqlConnection(@"Data Source=Stacy492\SQLEXPRESS;Initial Catalog=cndb;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand(@"INSERT INTO cncinfo (part,draftpath) VALUES ('" + file.Name + "','" + e.FullPath + "') ", con);
cmd.ExecuteNonQuery();
con.Close();
}
break;
case WatcherChangeTypes.Deleted:
//remove file from database
this.Invoke(addItemInList, "File: " + e.FullPath + " Deleted");
{
SqlConnection con = new SqlConnection(@"Data Source=Stacy492\SQLEXPRESS;Initial Catalog=cndb;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand(@"delete cncinfo where part='" + file.Name + "' ;", con);
cmd.ExecuteNonQuery();
con.Close();
}
break;
case WatcherChangeTypes.Changed:
///if you are storing file in database(not file name whole file in binary format)
///then you can update the file in database here
///this event will be fired when any data has changed in the file.
this.Invoke(addItemInList, "File: " + e.FullPath + " Changed");
{
SqlConnection con = new SqlConnection(@"Data Source=Stacy492\SQLEXPRESS;Initial Catalog=cndb;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand(@"update cncinfo set part='" + NotifyFilters.FileName
+ "',draftpath='" + e.FullPath + "' where part='" + file.Name + "'", con);
cmd.ExecuteNonQuery();
con.Close();
this.Validate();
}
break;
}
}

private void OnRenamed(object source, RenamedEventArgs e)
{
FileInfo file = new FileInfo(e.FullPath);
Console.WriteLine(file.Name);
//Update then old filename with new here
this.Invoke(addItemInList, string.Format("File: {0} renamed to {1}", e.OldFullPath, e.FullPath));

{
SqlConnection con = new SqlConnection(@"Data Source=Stacy492\SQLEXPRESS;Initial Catalog=cndb;Integrated Security=True");
con.Open();
//s.debasish79@gmail.com
SqlCommand cmd = new SqlCommand(@"update cncinfo set part='" + file.Name
+ "',draftpath='" + e.FullPath + "' where part='" + file.Name + "'", con);
cmd.ExecuteNonQuery();
con.Close();
this.Validate();
}


}

#region We are creating delegate to update the value in Windows Form Control
/// <summary>
/// We are using delegate invoke method to update the UI control outside the current thread.
/// otherwise it will throws the error.
/// you can also use this method to update the cell value in datagridview
/// </summary>
/// <param name="_string"></param>
private delegate void delAddItem(string _string);
private delAddItem addItemInList;
private void AddString(string _string)
{
listbox1.Items.Add(_string);
}
#endregion

}

最佳答案

要获取文件名,这将帮助您!!!!

    FileInfo file = new FileInfo(e.FullPath);
Console.WriteLine(file.Name);

关于c# - 如何在filewatcher中更新文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25715447/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com