【问题标题】:Rename a file in C# (NET 2.0) without moving it or using DOS / Visual Basic commands在 C# (NET 2.0) 中重命名文件而不移动它或使用 DOS/Visual Basic 命令
【发布时间】:2013-07-15 08:08:12
【问题描述】:

我还没有在 .NET 中为 C# 找到文件重命名函数,所以我有点困惑如何重命名文件。我在 Process.Start 中使用命令提示符,但这并不专业,每次都会弹出一个黑色的DOS 窗口。是的,我知道 Visual Basic 命名空间中有一些东西,但我无意将“visual-basic.dll”添加到我的项目中。

我发现了一些“移动”文件以重命名它的示例。这是一种非常痛苦的方法,也是一种劣质的解决方法。这样的步法我可以自己编程。

每种语言都有重命名命令,所以我很惊讶 C# 没有或者我还没有发现。什么是正确的命令?

对于大文件和在 CD 上重命名,此代码有效,但您的项目将部分转换为 Visual Basic(据我了解,可能不是这样):

//Add the Microsoft.VisualBasic.MyServices reference and namespace in a project;

//For directories:

 private static bool RenameDirectory(string DirPath, string NewName)
 {
     try
     {
         FileSystemProxy FileSystem = new Microsoft.VisualBasic.Devices.Computer().FileSystem;
         FileSystem.RenameDirectory(DirPath, NewName);
         FileSystem = null;
         return true;
     }
     catch {
        return false;
     } //Just shut up the error generator of Visual Studio
 }

 //For files:

 private static bool RenameFile(string FilePath, string NewName)
 {
     try
     {
         FileSystemProxy FileSystem = new Microsoft.VisualBasic.Devices.Computer().FileSystem;
         FileSystem.RenameFile(FilePath, NewName);
         FileSystem = null;
         return true;
     }
     catch {
         return false;
     } //Just shut up the error generator of Visual Studio
 }

【问题讨论】:

  • 请注意,VB 命名空间选项 (FileSystem.RenameFile) 是 .NET 框架的一部分,因此无需添加任何新内容 - 但是,它只是在内部调用 File.Move...跨度>

标签: c# .net file c#-2.0 rename


【解决方案1】:

重命名只是一个动作,反之亦然,请参阅 MSDN:File.Move

在操作系统中,所有意图和目的的操作都是相同的。这就是为什么在资源管理器中同一分区上的移动几乎是瞬时的 - 只需调整文件名和逻辑位置。要重命名同一目录中的文件,请将其移动到同一目录中的新文件名。

using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        string path = @"c:\temp\MyTest.txt";
        string path2 = @"c:\temp2\MyTest.txt";
        try 
        {
            if (!File.Exists(path)) 
            {
                // This statement ensures that the file is created,
                // but the handle is not kept.
                using (FileStream fs = File.Create(path)) {}
            }

            // Ensure that the target does not exist.
            if (File.Exists(path2)) 
            File.Delete(path2);

            // Move the file.
            File.Move(path, path2);
            Console.WriteLine("{0} was moved/renamed to {1}.", path, path2);

            // See if the original exists now.
            if (File.Exists(path)) 
            {
                Console.WriteLine("The original file still exists, which is unexpected.");
            } 
            else 
            {
                Console.WriteLine("The original file no longer exists, which is expected.");
            }           

        } 
        catch (Exception e) 
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}

【讨论】:

  • 我认为那些可重写的重命名需要记录会话级别的更改。所以重命名只是一个逻辑操作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-29
  • 1970-01-01
  • 2011-11-27
  • 1970-01-01
  • 2014-12-23
相关资源
最近更新 更多