using System.Collections;
using System.Collections.Generic;
using System.IO;
using System;
using System.Windows.Forms;
using UnityEngine;
namespace UIFrame{
public class FileOperatorTool
{
///
/// 打开windows open窗口
///
///
///
public static OpenFileDialog OpenFileForm(string _formTitle, string _filterLimit)
{
OpenFileDialog op = new OpenFileDialog();
op.Title = _formTitle;
op.Multiselect = true;
op.Filter = _filterLimit;
if (op.ShowDialog() == DialogResult.OK)
{
UnityEngine.Debug.Log("file name === " + op.FileName);
//添加上传方法
return op;
}
return op;
}
public static void OpenFolderBrowserDialog(string _formTitle, List
m_files) {
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.Description = _formTitle;
if (dialog.ShowDialog() == DialogResult.OK)
{
m_files.Clear();
string foldPath = dialog.SelectedPath;
if (Directory.Exists(foldPath))
{
string[] file_names = Directory.GetFiles(foldPath);
for (int i = 0; i < file_names.Length; i++)
{
m_files.Add(file_names[i]);
}
}
//ListFiles(new DirectoryInfo(foldPath), m_files);
//ProcFiles(m_files);
}
}
///
/// 打开windows save窗口
///
///
///
public static void SaveFileForm(string _formTitle, string _filterLimit)
{
SaveFileDialog sd = new SaveFileDialog();
sd.Title = _formTitle;
sd.Filter = _filterLimit;
if (sd.ShowDialog() == DialogResult.OK)
{
UnityEngine.Debug.Log("unload");
}
}
///
/// 是否存在该文件夹
///
/// 文件夹路径
///
public static bool IsExitDir(string _dirPath)
{
bool isExit = false;
if (string.IsNullOrEmpty(_dirPath))
{
Debug.Log("当前文件夹路径为空,_dirPath= " + _dirPath);
isExit = false;
}
else
{
if (!Directory.Exists(_dirPath))
{
Debug.Log("当前路径无该文件夹,检查_dirPath= " + _dirPath);
isExit = false;
}
else
{
isExit = true;
}
}
return isExit;
}
///
/// 是否存在该文件
///
/// 文件路径
///
public static bool IsExitFile(string _filePath)
{
bool isExit = false;
if (string.IsNullOrEmpty(_filePath))
{
Debug.Log("当前文件夹路径为空,_filePath= " + _filePath);
isExit = false;
}
else
{
if (!File.Exists(_filePath))
{
Debug.Log("当前路径无该文件夹,_filePath= " + _filePath);
isExit = false;
}
else
{
isExit = true;
}
}
return isExit;
}
///
/// 返回当前文件夹的第一个文件名
///
///
///
public static string GetDirFirstFileName(string _dirPath)
{
string firstNum = "";
if (string.IsNullOrEmpty(_dirPath))
{
Debug.Log("当前文件夹路径为空,_dirPath= " + _dirPath);
}
else
{
if (!Directory.Exists(_dirPath))
{
Debug.Log("当前路径无该文件夹,检查_dirPath= " + _dirPath);
}
else
{
string[] fileName = Directory.GetFiles(_dirPath);
string firstFileName = Path.GetFileNameWithoutExtension(fileName[0]);
firstNum = firstFileName;
}
}
return firstNum;
}
///
/// 返回当前文件夹的文件数量
///
///
///
public static int GetDirFilesCount(string _dirPath)
{
int totalNum = 0;
if (string.IsNullOrEmpty(_dirPath))
{
Debug.Log("当前文件夹路径为空,_dirPath= " + _dirPath);
}
else
{
if (!Directory.Exists(_dirPath))
{
Debug.Log("当前路径无该文件夹,检查_dirPath= " + _dirPath);
}
else
{
string[] fileName = Directory.GetFiles(_dirPath);
totalNum = fileName.Length;
}
}
return totalNum;
}
///
/// 删除文件夹中的所有文件类型
///
///
public static void DeletDirectory(string _dirPth)
{
if (Directory.Exists(_dirPth))
{
//删除files
string[] files = Directory.GetFiles(_dirPth);
string[] subDir = Directory.GetDirectories(_dirPth);
if (files.Length > 0)
{
foreach (string item in files)
{
File.Delete(item);
}
}
//删除sub directory
if (subDir.Length > 0)
{
foreach (string item in subDir)
{
if (Directory.Exists(item))
{
DeletDirectory(item);
}
}
}
}
else
{
Debug.Log("文件夹路径出错,检查路径_dirPth = " + _dirPth);
}
}
///
/// 删除整个文件夹
///
///
public static void DeletDir(string _dirPth)
{
if (Directory.Exists(_dirPth))
{
Directory.Delete(_dirPth, true);
}
}
///
/// 删除文件夹中指定文件名的文件
///
/// 文件夹名
/// 指定文件名
public static void DeletSpecifiedFile(string _dirPath,string _specifiedName)
{
//string[] spe_paths = Directory.GetFiles(_dirPath, _specifiedName, SearchOption.AllDirectories);
if (Directory.Exists(_dirPath))
{
string[] spe_paths = Directory.GetFiles(_dirPath);
foreach (string item in spe_paths)
{
if (File.Exists(item) && item.Contains(_specifiedName))
{
File.Delete(item);
}
}
}
}
///
/// 拷贝文件夹
///
///
///
public static void CopyDirectory(string srcdir, string desdir)
{
string folderName = srcdir.Substring(srcdir.LastIndexOf("\\") + 1);
string desfolderdir = desdir + "\\" + folderName;
if (desdir.LastIndexOf("\\") == (desdir.Length - 1))
{
desfolderdir = desdir + folderName;
}
string[] filenames = Directory.GetFileSystemEntries(srcdir);
foreach (string file in filenames)// 遍历所有的文件和目录
{
if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
{
string currentdir = desfolderdir + "\\" + file.Substring(file.LastIndexOf("\\") + 1);
if (!Directory.Exists(currentdir))
{
Directory.CreateDirectory(currentdir);
}
CopyDirectory(file, desfolderdir);
}
else // 否则直接copy文件
{
string srcfileName = file.Substring(file.LastIndexOf("\\") + 1);
srcfileName = desfolderdir + "\\" + srcfileName;
if (!Directory.Exists(desfolderdir))
{
Directory.CreateDirectory(desfolderdir);
}
File.Copy(file, srcfileName);
}
}
}
}
}