如何在FileWatcher中更新文件名

用户名

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

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

此外,我发现了一个重要的错误。每当我使用该按钮将文件从cell [1]移到cell [2]时,我就会发现该错误。整个文件被移至cell [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();
            //[email protected]
            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); 

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

更新文件时如何在载波中保留相同的文件名(在Rails中)

来自分类Dev

Vim更新文件名

来自分类Dev

检查文件名是否存在并在Google脚本中更新文件名

来自分类Dev

如何编码我的程序来为目录中的每个新文件创建新文件名?

来自分类Dev

如何在Laravel 4中更新文件上传?

来自分类Dev

如何在Git中更新文件写入权限

来自分类Dev

如何在QFileSystemModel中更新文件权限

来自分类Dev

如何在PHP中更新文件

来自分类Dev

如何在bash中创建一个在其他函数之间插入新文件名的函数?

来自分类Dev

当程序在cshell中循环运行时,如何给新文件名?

来自分类Dev

Notepad ++新文件名

来自分类Dev

如何从多个 CSV 文件中提取行并将文件名添加到新文件中

来自分类Dev

如何在现有的tar.gz归档文件中添加/更新文件?

来自分类Dev

如何生成新的 GUID,然后使用该 GUID 作为文件名写入指定路径中的新文件?

来自分类Dev

如何在Mongo端更新文件?

来自分类Dev

当文件名包含从Excel表引用的特定字符串时更新文件名

来自分类Dev

如何在Google Drive V3 PHP中更新文件

来自分类Dev

如何在Windows 10中更新文件夹的副本/备份?

来自分类Dev

如何在index.php页面上的ajax中调用更新文件

来自分类Dev

如何替换或更新文件中的json值?

来自分类Dev

如何更新文件中的每一行

来自分类Dev

使用文件名读取最新文件包含python中的字符串

来自分类Dev

获取基于文件名python的最新文件

来自分类Dev

将文件名复制到新文件

来自分类Dev

如何在Java中获取目录的最新文件

来自分类Dev

如何在Moc中刷新文件列表?

来自分类Dev

如何在Mutt中创建新文件夹

来自分类Dev

如何在Windows FTP终端中创建新文件

来自分类Dev

重命名到数据库后如何插入/保存新文件名?

Related 相关文章

  1. 1

    更新文件时如何在载波中保留相同的文件名(在Rails中)

  2. 2

    Vim更新文件名

  3. 3

    检查文件名是否存在并在Google脚本中更新文件名

  4. 4

    如何编码我的程序来为目录中的每个新文件创建新文件名?

  5. 5

    如何在Laravel 4中更新文件上传?

  6. 6

    如何在Git中更新文件写入权限

  7. 7

    如何在QFileSystemModel中更新文件权限

  8. 8

    如何在PHP中更新文件

  9. 9

    如何在bash中创建一个在其他函数之间插入新文件名的函数?

  10. 10

    当程序在cshell中循环运行时,如何给新文件名?

  11. 11

    Notepad ++新文件名

  12. 12

    如何从多个 CSV 文件中提取行并将文件名添加到新文件中

  13. 13

    如何在现有的tar.gz归档文件中添加/更新文件?

  14. 14

    如何生成新的 GUID,然后使用该 GUID 作为文件名写入指定路径中的新文件?

  15. 15

    如何在Mongo端更新文件?

  16. 16

    当文件名包含从Excel表引用的特定字符串时更新文件名

  17. 17

    如何在Google Drive V3 PHP中更新文件

  18. 18

    如何在Windows 10中更新文件夹的副本/备份?

  19. 19

    如何在index.php页面上的ajax中调用更新文件

  20. 20

    如何替换或更新文件中的json值?

  21. 21

    如何更新文件中的每一行

  22. 22

    使用文件名读取最新文件包含python中的字符串

  23. 23

    获取基于文件名python的最新文件

  24. 24

    将文件名复制到新文件

  25. 25

    如何在Java中获取目录的最新文件

  26. 26

    如何在Moc中刷新文件列表?

  27. 27

    如何在Mutt中创建新文件夹

  28. 28

    如何在Windows FTP终端中创建新文件

  29. 29

    重命名到数据库后如何插入/保存新文件名?

热门标签

归档