C#/.NET-文件夹&文件重命名(非复制copy+删除delete)

用法如下

1                 //文件夹重命名
2                 Computer myComputer = new Computer();
3                 myComputer.FileSystem.RenameFile(fullFilePath, newFileName);
4                 myComputer.FileSystem.RenameDirectory(fullDirPath, newName);        

解释一下用法,

fullFilePath 是文件的完整路径,

fullDirPath 是文件夹的完整路径;

newFileName 和 newName 是要改的名字,不需要加路径。

看一下文档,如下:

RenameFile:

 1     /// <summary>Renames a file.</summary>
 2     /// <param name="file">File to be renamed.</param>
 3     /// <param name="newName">New name of file.</param>
 4     /// <exception cref="T:System.ArgumentException">
 5     /// <paramref name="newName" /> contains path information or ends with a backslash (\).</exception>
 6     /// <exception cref="T:System.ArgumentNullException">
 7     ///         <paramref name="file" /> is <see langword="Nothing" />.
 8     /// -or-
 9     /// <paramref name="newName" /> is <see langword="Nothing" /> or an empty string.</exception>
10     /// <exception cref="T:System.IO.FileNotFoundException">The file does not exist.</exception>
11     /// <exception cref="T:System.IO.IOException">There is an existing file or directory with the name specified in <paramref name="newName" />.</exception>
12     /// <exception cref="T:System.IO.PathTooLongException">The path exceeds the system-defined maximum length.</exception>
13     /// <exception cref="T:System.NotSupportedException">A file or directory name in the path contains a colon (:) or is in an invalid format.</exception>
14     /// <exception cref="T:System.Security.SecurityException">The user lacks necessary permissions to view the path.</exception>
15     /// <exception cref="T:System.UnauthorizedAccessException">The user does not have required permission.</exception>
16     public void RenameFile(string file, string newName)
17     {
18       Microsoft.VisualBasic.FileIO.FileSystem.RenameFile(file, newName);
19     }

RenameDirectory:

 1     /// <summary>Renames a directory.</summary>
 2     /// <param name="directory">Path and name of directory to be renamed.</param>
 3     /// <param name="newName">New name for directory.</param>
 4     /// <exception cref="T:System.ArgumentException">
 5     /// <paramref name="newName" /> contains path information.</exception>
 6     /// <exception cref="T:System.ArgumentNullException">
 7     ///         <paramref name="directory" /> is <see langword="Nothing" />.
 8     /// -or-
 9     /// <paramref name="newName" /> is <see langword="Nothing" /> or an empty string.</exception>
10     /// <exception cref="T:System.IO.DirectoryNotFoundException">The directory does not exist.</exception>
11     /// <exception cref="T:System.IO.IOException">There is an existing file or directory with the name specified in <paramref name="newName" />.</exception>
12     /// <exception cref="T:System.IO.PathTooLongException">The path exceeds the system-defined maximum length.</exception>
13     /// <exception cref="T:System.NotSupportedException">A file or directory name in the path contains a colon (:) or is in an invalid format.</exception>
14     /// <exception cref="T:System.Security.SecurityException">The user lacks necessary permissions to view the path.</exception>
15     /// <exception cref="T:System.UnauthorizedAccessException">The user does not have required permission.</exception>
16     public void RenameDirectory(string directory, string newName)
17     {
18       Microsoft.VisualBasic.FileIO.FileSystem.RenameDirectory(directory, newName);
19     }

使用时,需要添加引用

1 using Microsoft.VisualBasic.Devices;

以及下图的,Microsoft.VisualBasic也是必不可少的。

 

posted on 2021-05-13 18:29  奥奥没有利  阅读(1819)  评论(0)    收藏  举报