重命名C#(NET 2.0)中的文件而不移动它或使用DOS / Visual Basic命令 [英] Rename a file in C# (NET 2.0) without moving it or using DOS / Visual Basic commands

查看:92
本文介绍了重命名C#(NET 2.0)中的文件而不移动它或使用DOS / Visual Basic命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#的.NET中我还没有找到文件重命名功能,因此我对如何重命名文件有些困惑。我将命令提示符与Process.Start一起使用,但这并不是很专业,黑色的 DOS 每次都会弹出一个窗口。是的,我知道Visual Basic命名空间中有一些东西,但这不是我要将 visual-basic.dll添加到我的项目中的意图。

I have not found yet a file-rename-function in .NET for C#, so I'm a bit confused how I would rename a file. I use the command prompt with Process.Start, but this isn't really professional and a black DOS window is popping up each time. Yes, I know there is something in the Visual Basic namespace, but this is not my intention to add the "visual-basic.dll" to my project.

我发现一些移动文件重命名的示例。这是一种非常痛苦的方法,并且是解决问题的伪劣解决方案。我可以自己编写程序。

I found some examples which "move" the file to rename it. It is a quite painful method and a shoddy workaround for things. Such footwork I can program myself.

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

Every language has renaming commands, so I am stunned that C# hasn't or I haven't found out yet. What is the right command?

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

For large files and to rename on CD, this code works, but your project will be partly converted into Visual Basic (as I understand it, maybe it is not so):

//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
 }


推荐答案

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

A rename is just a move and vice versa, see the MSDN : File.Move

在OS中,对于所有目的而言,操作都是相同的。这就是在资源管理器中几乎立即在同一分区上移动的原因-只需调整文件名和逻辑位置即可。要重命名同一目录中的文件,请将其移至同一目录中的新文件名。

In the OS the operations are the same for all intents an purposes. That's why in explorer a move on the same partition is near instantaneous - just adjusts the file name and logical location. To Rename a file in the same directory you Move it to a new File Name in the same directory.

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());
        }
    }
}

这篇关于重命名C#(NET 2.0)中的文件而不移动它或使用DOS / Visual Basic命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆