如何在filewatcher中更新文件名

本文关键字:更新 文件名 filewatcher | 更新日期: 2023-09-27 18:20:55

最后,我为自动更新进行了编码。该文件将添加到数据库中。但是我无法更新列0中的文件名。它只提供第一次文件名。当我将文件名更改为其他名称时,它不会出现在第0列中。

我只需要在part中输入特定的文件名。我不知道如何修剪文件名并在第0列中让步。请帮帮我。

此外,我在其中发现了一个重要的错误。。当我使用按钮将文件从单元格[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
}

}

如何在filewatcher中更新文件名

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

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