手动重命名文件,然后在数据库C#中重命名

时间:2013-07-01 07:53:10

标签: c#

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

使用

时无法正常工作
name=Path.GetFileNameWithoutExtension(filepath)

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

如何解决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.
            }
        }
    }

0 个答案:

没有答案