Accounting/Assets/Scripts/TranslateFiles.cs

118 lines
3.7 KiB
C#
Raw Normal View History

2023-09-24 21:35:44 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Android;
public class TranslateFiles : MonoBehaviour
{
[SerializeField] private UnityEngine.UI.Button _btn1;
[SerializeField] private UnityEngine.UI.Button _btn2;
[SerializeField] private UnityEngine.UI.Text _text;
[SerializeField] private string _pkgName = "com.cxgame.zscwmw";
[SerializeField]
private string _userPath = "/storage/emulated/0/Pictures/";
// Start is called before the first frame update
void Start()
{
_btn1.onClick.RemoveAllListeners();
_btn2.onClick.RemoveAllListeners();
_btn1.onClick.AddListener(() =>
{
try
{
string srcPath = Path.Combine("/data/data/", _pkgName);
string destPath = Path.Combine(_userPath, _pkgName);
CopyFolder(srcPath, destPath);
_text.text += "移动到用户文件夹成功"+ "\n";
}
catch (Exception e)
{
_text.text += e.ToString() + "\n";
}
});
_btn2.onClick.AddListener(() =>
{
try
{
string srcPath = Path.Combine(_userPath, _pkgName);
string destPath = Path.Combine("/data/data/", _pkgName);
CopyFolder(srcPath, destPath);
_text.text += "移动到根目录成功"+ "\n";
}
catch (Exception e)
{
_text.text += e.ToString() + "\n";
}
});
}
/// <summary>
/// 移动文件夹中的所有文件夹与文件到另一个文件夹 //转载请注明来自 http://www.uzhanbao.com
/// </summary>
/// <param name="sourcePath">源文件夹</param>
/// <param name="destPath">目标文件夹</param>
public static void CopyFolder(string sourcePath, string destPath)
{
if (Directory.Exists(sourcePath))
{
if (!Directory.Exists(destPath))
{
//目标目录不存在则创建
try
{
Directory.CreateDirectory(destPath);
}
catch (Exception ex)
{
throw new Exception("创建目标目录失败:" + ex.Message);
}
}
else
{
foreach (var fileInfo in new DirectoryInfo(destPath).GetFiles())
{
fileInfo.Delete();
}
}
//获得源文件下所有文件
List<string> files = new List<string>(Directory.GetFiles(sourcePath));
files.ForEach(c =>
{
string destFile = Path.Combine(new string[] {destPath, Path.GetFileName(c)});
//覆盖模式
if (File.Exists(destFile))
{
File.Delete(destFile);
}
File.Copy(c, destFile);
});
//获得源文件下所有目录文件
List<string> folders = new List<string>(Directory.GetDirectories(sourcePath));
folders.ForEach(c =>
{
string destDir = Path.Combine(new string[] {destPath, Path.GetFileName(c)});
//Directory.Move必须要在同一个根目录下移动才有效不能在不同卷中移动。
//Directory.Move(c, destDir);
//采用递归的方法实现
CopyFolder(c, destDir);
});
}
else
{
throw new DirectoryNotFoundException("源目录不存在!");
}
}
private void JsonTest()
{
}
}