gpt4 book ai didi

C# 文件并从 1 自动重命名?

转载 作者:太空宇宙 更新时间:2023-11-03 15:25:15 25 4
gpt4 key购买 nike

我用 c# 编写了这段代码,用于更改文件夹的位置和该文件夹的所有子文件夹。我想在将其复制到目的地时更改所有文件夹的名称。例如,我想将名称从 1 更改为...。我应该如何更改我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace Folder
{
class clssMoveFolder
{

string temppath;
public string StrtxtSource;
public string destDirName;

public void Directorycopy(string sourceDirName, string destDirName, bool cutSubDirs, string strExtension)
{
try
{

DirectoryInfo dir = new DirectoryInfo(sourceDirName);
DirectoryInfo[] dirs = dir.GetDirectories();
// If the source directory does not exist, throw an exception.

if (!dir.Exists)
{
throw new DirectoryNotFoundException("Source directory does not exist or could not be found: " + sourceDirName);
}
FileInfo[] files = dir.GetFiles();
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}
//Get the file contents of the directory to copy.
for (int a = 0; a < files.Length; ++a)
{
// Create the path to the new copy of the file.
if (files[a].Extension == "."+strExtension )
{
temppath = Path.Combine(destDirName, files[a].Name);
files[a].MoveTo(temppath);
}
else if (files[a].Extension == strExtension)
{
temppath = Path.Combine(destDirName, files[a].Name);
files[a].MoveTo(temppath);
}
else if (strExtension == "AllFiles")
{
temppath = Path.Combine(destDirName, files[a].Name);
files[a].MoveTo(temppath);
}
else
files[a].Delete();
dir.Refresh();
}
FileInfo[] filesCheck = dir.GetFiles();
if (dirs.Length == 0 && filesCheck.Length == 0 && dir.Name != StrtxtSource)
{
dir.Delete();
}

// If copySubDirs is true, copy the subdirectories.
if (cutSubDirs)
{

foreach (DirectoryInfo subdir in dirs)
{

// Copy the subdirectories.

string temppath= Path.Combine(destDirName, subdir.Name);

Directorycopy(subdir.FullName, temppath,cutSubDirs,strExtension);
}
}
}
catch (Exception e)
{

MessageBox.Show(e.ToString());
}
}

}
}

最佳答案

如果您只是想要在复制后重命名文件,您可以直接以不同的名称复制它。例如。而不是复制 a.txt 并将其重命名为 b.txt 之后,您可以将原始 a.txt 复制为 b .txt 到目标目录。

重命名可以通过使用 File.Move 来完成。方法。

File.Move("a.txt", "b.txt");

如果您希望能够执行此操作,即使其他人将文件复制到目录中(例如,通过使用 Windows 资源管理器或其他应用程序编写的文件),您可能需要更仔细地了解看看 FileSystemWatcher .您可以使用它来监视指定目录(可选地包括它的子目录)中的更改并对这些更改使用react。

...
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"D:\SomeTestDirectory";
watcher.Filter = "*.*";
watcher.NotifyFilter = NotifyFilters.FileName; // Trigger only on events associated with the filename
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true; // Starts the "watching"
...

private static void OnChanged(object source, FileSystemEventArgs e)
{
// Do whatever you want here, e.g. rename the file.
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
}

关于C# 文件并从 1 自动重命名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35601585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com