1

我想重命名数据库中的文件(用于文件信息)

我使用时它不起作用

name=Path.GetFileNameWithoutExtension(filepath)

我认为问题在于他仍然使用带有扩展e.OldName名的旧名称,而在数据库中它没有扩展名。

我该如何解决e.OldName没有扩展的问题?和我做的一样e.Name吗?

好的,我得到了这段代码,但它不起作用:

  private void fileSystemWatcher1_Renamed(object sender, System.IO.RenamedEventArgs e)
    {
        filepath = Path.Combine(source, e.Name);
        oldfilepath = Path.Combine(source, e.OldName);
        Console.WriteLine(e.OldName + " => " + e.Name);
        listBox1.Items.Add("File renamed> " + e.FullPath + " -Date:" + DateTime.Now);
        name = Path.GetFileNameWithoutExtension(filepath);
        oldfilename = Path.GetFileNameWithoutExtension(oldfilepath);
        extension = Path.GetExtension(e.FullPath);
        size = e.Name.Length;
        query = "update files set name='"+name+"' where name='"+oldfilename +"'";
        query();
    }

    static void query()
    {
        String database1 = ConfigurationManager.AppSettings[@"Database1"];
        var connection = new MySqlConnection(database1);
        connection.Open();
        MySqlCommand cmd = new MySqlCommand(query, connection);
        cmd.ExecuteNonQuery();
        connection.Close();
    }


        private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
    {
        filepath = Path.Combine(source, e.Name );
        listBox1.Items.Add("File created> " + e.FullPath + " -Date:" + DateTime.Now);
        name = Path.GetFileNameWithoutExtension(filepath);
        extension = Path.GetExtension(e.FullPath);
        size = e.Name.Length;
        query = "INSERT INTO files (name,size,last_edit,extension) VALUES('" + name + "','" + size + "',now(),'" + extension + "')";    
        query();

        if (Directory.Exists(e.FullPath))
        {
            copyfolder();
            Directory.CreateDirectory(target);
        }
        else
        {

            if (WaitForFileAvailable(e.FullPath, TimeSpan.FromSeconds(10)))
            {
                var file = Path.Combine(source, e.Name);
                var copy_file = Path.Combine(target, e.Name);
                var destination = Path.Combine(target, Path.ChangeExtension(source, Path.GetExtension(source)));

                if (File.Exists(file))
                {
                    File.Delete(copy_file);
                    File.Copy(e.FullPath, Path.Combine(target, e.Name));
                }
                else
                {
                    File.Copy(e.FullPath, Path.Combine(target, e.Name));
                }
            }
            else // The file failed to become available within 10 seconds.
            {
                // Error handling.
            }
        }
    }
4

0 回答 0