Unity 框架

SystemFileTools.cs 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Runtime.InteropServices;
  6. using UnityEngine;
  7. namespace TFramework
  8. {
  9. public class SystemFileTools
  10. {
  11. #region OpenFile数据接收类
  12. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  13. public class OpenDialogFile
  14. {
  15. public int structSize = 0;
  16. public IntPtr dlgOwner = IntPtr.Zero;
  17. public IntPtr instance = IntPtr.Zero;
  18. public String filter = null;
  19. public String customFilter = null;
  20. public int maxCustFilter = 0;
  21. public int filterIndex = 0;
  22. public String file = null;
  23. public int maxFile = 0;
  24. public String fileTitle = null;
  25. public int maxFileTitle = 0;
  26. public String initialDir = null;
  27. public String title = null;
  28. public int flags = 0;
  29. public short fileOffset = 0;
  30. public short fileExtension = 0;
  31. public String defExt = null;
  32. public IntPtr custData = IntPtr.Zero;
  33. public IntPtr hook = IntPtr.Zero;
  34. public String templateName = null;
  35. public IntPtr reservedPtr = IntPtr.Zero;
  36. public int reservedInt = 0;
  37. public int flagsEx = 0;
  38. }
  39. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  40. public class OpenDialogDir
  41. {
  42. public IntPtr hwndOwner = IntPtr.Zero;
  43. public IntPtr pidlRoot = IntPtr.Zero;
  44. public String pszDisplayName = null;
  45. public String lpszTitle = null;
  46. public UInt32 ulFlags = 0;
  47. public IntPtr lpfn = IntPtr.Zero;
  48. public IntPtr lParam = IntPtr.Zero;
  49. public int iImage = 0;
  50. }
  51. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  52. public class OpenFileName
  53. {
  54. public int structSize = 0;
  55. public IntPtr dlgOwner = IntPtr.Zero;
  56. public IntPtr instance = IntPtr.Zero;
  57. public String filter = null;
  58. public String customFilter = null;
  59. public int maxCustFilter = 0;
  60. public int filterIndex = 0;
  61. public String file = null;
  62. public int maxFile = 0;
  63. public String fileTitle = null;
  64. public int maxFileTitle = 0;
  65. public String initialDir = null;
  66. public String title = null;
  67. public int flags = 0;
  68. public short fileOffset = 0;
  69. public short fileExtension = 0;
  70. public String defExt = null;
  71. public IntPtr custData = IntPtr.Zero;
  72. public IntPtr hook = IntPtr.Zero;
  73. public String templateName = null;
  74. public IntPtr reservedPtr = IntPtr.Zero;
  75. public int reservedInt = 0;
  76. public int flagsEx = 0;
  77. }
  78. #endregion
  79. public static class FolderBrowserHelper
  80. {
  81. #region Window
  82. [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
  83. private static extern bool GetOpenFileName([In, Out] OpenFileName ofn);
  84. [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
  85. private static extern bool GetSaveFileName([In, Out] OpenFileName ofn);
  86. [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
  87. private static extern bool GetOpenFileName([In, Out] OpenDialogFile ofn);
  88. [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
  89. private static extern bool GetSaveFileName([In, Out] OpenDialogFile ofn);
  90. [DllImport("shell32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
  91. private static extern IntPtr SHBrowseForFolder([In, Out] OpenDialogDir ofn);
  92. [DllImport("shell32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
  93. private static extern bool SHGetPathFromIDList([In] IntPtr pidl, [In, Out] char[] fileName);
  94. #endregion
  95. public const string IMAGEFILTER = "图片文件(*.jpg;*.png)\0*.jpg;*.png";
  96. public const string ALLFILTER = "所有文件(*.*)\0*.*";
  97. /// <summary>
  98. /// 选择文件返回文件路径
  99. /// </summary>
  100. /// <param name="filter">文件类型筛选器</param>
  101. public static string SelectFile(string filter = ALLFILTER)
  102. {
  103. try
  104. {
  105. OpenFileName openFileName = new OpenFileName();
  106. openFileName.structSize = Marshal.SizeOf(openFileName);
  107. openFileName.initialDir = Application.streamingAssetsPath.Replace('/', '\\');
  108. openFileName.filter = filter;
  109. openFileName.file = new string(new char[256]);
  110. openFileName.maxFile = openFileName.file.Length;
  111. openFileName.fileTitle = new string(new char[64]);
  112. openFileName.maxFileTitle = openFileName.fileTitle.Length;
  113. openFileName.title = "选择文件";
  114. openFileName.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008;
  115. if (GetSaveFileName(openFileName))
  116. {
  117. string filepath = openFileName.file; //选择的文件路径;
  118. if (File.Exists(filepath))
  119. {
  120. return filepath;
  121. }
  122. }
  123. }
  124. catch (Exception e)
  125. {
  126. Debug.LogError(e);
  127. }
  128. return "";
  129. }
  130. /// <summary>
  131. /// 返回所选文件夹路径
  132. /// </summary>
  133. /// <param name="dialogtitle">打开对话框的标题</param>
  134. /// <returns>所选文件夹路径</returns>
  135. public static string GetPathFromWindowsExplorer(string dialogtitle = "请选择文件路径")
  136. {
  137. try
  138. {
  139. OpenDialogDir ofn2 = new OpenDialogDir();
  140. ofn2.pszDisplayName = new string(new char[2048]);
  141. // 存放目录路径缓冲区
  142. ofn2.lpszTitle = dialogtitle; // 标题
  143. //ofn2.ulFlags = 0x00000040;
  144. ofn2.ulFlags = 0x00080000; // 新的样式,带编辑框
  145. IntPtr pidlPtr = SHBrowseForFolder(ofn2);
  146. char[] charArray = new char[2048];
  147. for (int i = 0; i < 2048; i++)
  148. {
  149. charArray[i] = '\0';
  150. }
  151. SHGetPathFromIDList(pidlPtr, charArray);
  152. string res = new string(charArray);
  153. res = res.Substring(0, res.IndexOf('\0'));
  154. return res;
  155. }
  156. catch (Exception e)
  157. {
  158. Debug.LogError(e);
  159. }
  160. return string.Empty;
  161. }
  162. public static void CopyDirectory(string srcPath, string destPath)
  163. {
  164. try
  165. {
  166. DirectoryInfo dir = new DirectoryInfo(srcPath);
  167. FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //获取目录下(不包含子目录)的文件和子目录
  168. foreach (FileSystemInfo i in fileinfo)
  169. {
  170. if (i is DirectoryInfo) //判断是否文件夹
  171. {
  172. if (!Directory.Exists(destPath + "\\" + i.Name))
  173. {
  174. Directory.CreateDirectory(destPath + "\\" + i.Name); //目标目录下不存在此文件夹即创建子文件夹
  175. }
  176. CopyDirectory(i.FullName, destPath + "\\" + i.Name); //递归调用复制子文件夹
  177. }
  178. else
  179. {
  180. File.Copy(i.FullName, destPath + "\\" + i.Name, true); //不是文件夹即复制文件,true表示可以覆盖同名文件
  181. }
  182. }
  183. }
  184. catch (Exception e)
  185. {
  186. throw;
  187. }
  188. }
  189. }
  190. }
  191. }