51 lines
1.6 KiB
Markdown
51 lines
1.6 KiB
Markdown
#unity/日常积累
|
|
|
|
复制一个文件夹的内容,代码参考
|
|
|
|
``` cs
|
|
public static void CopyDirectory(string sourceDir, string targetDir)
|
|
{
|
|
DirectoryInfo dir = new DirectoryInfo(sourceDir);
|
|
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: {sourceDir}");
|
|
}
|
|
|
|
// If the destination directory does not exist, create it.
|
|
if (!Directory.Exists(targetDir))
|
|
{
|
|
Directory.CreateDirectory(targetDir);
|
|
}
|
|
|
|
// Get the files in the directory and copy them to the new location.
|
|
FileInfo[] files = dir.GetFiles();
|
|
foreach (FileInfo file in files)
|
|
{
|
|
string tempPath = Path.Combine(targetDir, file.Name);
|
|
file.CopyTo(tempPath, false);
|
|
}
|
|
|
|
// If copying subdirectories, copy them and their contents to the new location.
|
|
foreach (DirectoryInfo subdir in dirs)
|
|
{
|
|
string tempPath = Path.Combine(targetDir, subdir.Name);
|
|
CopyDirectory(subdir.FullName, tempPath);
|
|
}
|
|
}
|
|
```
|
|
|
|
如果是需要整个文件夹替换,执行上述操作之前,可以先判空,然后删除文件夹
|
|
|
|
``` cs
|
|
if (Directory.Exists(RemoteHotUpdateRootPath))
|
|
{
|
|
Directory.Delete(RemoteHotUpdateRootPath, true);
|
|
}
|
|
```
|
|
|
|
完整调用,就是删除文件夹,然后再复制文件夹过去即可
|
|
|
|
![[Pasted image 20240911192759.png]] |