FolderBrowserHelper.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. using SFB;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.IO;
  7. using System.Runtime.InteropServices;
  8. using UnityEditor;
  9. using UnityEngine;
  10. using UnityEngine.Networking;
  11. #region 调用系统窗口选择文件夹或文件
  12. public class FileOpMsg
  13. {
  14. public bool success;
  15. public string msg;
  16. }
  17. #region 结构体
  18. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  19. public class OpenDialogFile
  20. {
  21. public int structSize = 0;
  22. public IntPtr dlgOwner = IntPtr.Zero;
  23. public IntPtr instance = IntPtr.Zero;
  24. public String filter = null;
  25. public String customFilter = null;
  26. public int maxCustFilter = 0;
  27. public int filterIndex = 0;
  28. public String file = null;
  29. public int maxFile = 0;
  30. public String fileTitle = null;
  31. public int maxFileTitle = 0;
  32. public String initialDir = null;
  33. public String title = null;
  34. public int flags = 0;
  35. public short fileOffset = 0;
  36. public short fileExtension = 0;
  37. public String defExt = null;
  38. public IntPtr custData = IntPtr.Zero;
  39. public IntPtr hook = IntPtr.Zero;
  40. public String templateName = null;
  41. public IntPtr reservedPtr = IntPtr.Zero;
  42. public int reservedInt = 0;
  43. public int flagsEx = 0;
  44. }
  45. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  46. public class OpenDialogDir
  47. {
  48. public IntPtr hwndOwner = IntPtr.Zero;
  49. public IntPtr pidlRoot = IntPtr.Zero;
  50. public String pszDisplayName = null;
  51. public String lpszTitle = null;
  52. public UInt32 ulFlags = 0;
  53. public IntPtr lpfn = IntPtr.Zero;
  54. public IntPtr lParam = IntPtr.Zero;
  55. public int iImage = 0;
  56. }
  57. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
  58. public class OpenFileName
  59. {
  60. public int structSize = 0;
  61. //public IntPtr hwndOwner = IntPtr.Zero;
  62. public IntPtr dlgOwner = IntPtr.Zero;
  63. public IntPtr instance = IntPtr.Zero;
  64. public String filter = null;
  65. public String customFilter = null;
  66. public int maxCustFilter = 0;
  67. public int filterIndex = 0;
  68. public String file = null;
  69. public int maxFile = 0;
  70. public String fileTitle = null;
  71. public int maxFileTitle = 0;
  72. public String initialDir = null;
  73. public String title = null;
  74. public int flags = 0;
  75. public short fileOffset = 0;
  76. public short fileExtension = 0;
  77. public String defExt = null;
  78. public IntPtr custData = IntPtr.Zero;
  79. public IntPtr hook = IntPtr.Zero;
  80. public String templateName = null;
  81. public IntPtr reservedPtr = IntPtr.Zero;
  82. public int reservedInt = 0;
  83. public int flagsEx = 0;
  84. }
  85. #endregion
  86. public class FolderBrowserHelper
  87. {
  88. #region 引用windows的dll文件
  89. //[DllImport("user32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
  90. //private static extern bool ClipCursor([In, Out] Rect rect);
  91. [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
  92. private static extern bool GetOpenFileName([In, Out] OpenFileName ofn);
  93. [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
  94. private static extern bool GetSaveFileName([In, Out] OpenFileName ofn);
  95. [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
  96. private static extern bool GetOpenFileName([In, Out] OpenDialogFile ofn);
  97. [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
  98. private static extern bool GetSaveFileName([In, Out] OpenDialogFile ofn);
  99. [DllImport("shell32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
  100. private static extern IntPtr SHBrowseForFolder([In, Out] OpenDialogDir ofn);
  101. [DllImport("shell32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
  102. private static extern bool SHGetPathFromIDList([In] IntPtr pidl, [In, Out] char[] fileName);
  103. #endregion
  104. #region 文件操作方法
  105. public const string IMAGEFILTER = "图片文件(*.jpg;*.png)\0*.jpg;*.png";
  106. public const string ALLFILTER = "所有文件(*.*)\0*.*";
  107. /// <summary>
  108. /// 选择文件
  109. /// </summary>
  110. /// <param name="callback">返回选择文件夹的路径</param>
  111. /// <param name="filter">文件类型筛选器</param>
  112. public static void SelectFile(Action<FileOpMsg> callback, string filter = ALLFILTER)
  113. {
  114. Debug.Log("open 00");
  115. FileOpMsg fileOpMsg = new FileOpMsg();
  116. try
  117. {
  118. OpenFileName openFileName = new OpenFileName();
  119. openFileName.structSize = Marshal.SizeOf(openFileName);
  120. //openFileName.hwndOwner = WindowsTools.ProcessHandle;
  121. openFileName.filter = filter;
  122. openFileName.file = new string(new char[256]);
  123. openFileName.maxFile = openFileName.file.Length;
  124. openFileName.fileTitle = new string(new char[64]);
  125. openFileName.maxFileTitle = openFileName.fileTitle.Length;
  126. openFileName.title = "选择文件";
  127. openFileName.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000008;
  128. if (GetSaveFileName(openFileName))
  129. {
  130. string filepath = openFileName.file; //选择的文件路径;
  131. if (File.Exists(filepath))
  132. {
  133. fileOpMsg.success = true;
  134. fileOpMsg.msg = filepath;
  135. callback?.Invoke(fileOpMsg);
  136. return;
  137. }
  138. }
  139. }
  140. catch (Exception e)
  141. {
  142. fileOpMsg.success = false;
  143. fileOpMsg.msg = e.ToString();
  144. callback?.Invoke(fileOpMsg);
  145. Debug.LogError(fileOpMsg.msg);
  146. return;
  147. }
  148. fileOpMsg.success = true;
  149. fileOpMsg.msg = string.Empty;
  150. callback?.Invoke(fileOpMsg);
  151. }
  152. /// <summary>
  153. /// 调用WindowsExploer并返回所选文件夹路径
  154. /// </summary>
  155. /// <param name="dialogtitle">打开对话框的标题</param>
  156. /// <returns>所选文件夹路径</returns>
  157. public static string GetPathFromWindowsExplorer(string dialogtitle = "请选择下载路径")
  158. {
  159. try
  160. {
  161. OpenDialogDir ofn2 = new OpenDialogDir();
  162. ofn2.pszDisplayName = new string(new char[2048]);// 存放目录路径缓冲区
  163. ofn2.lpszTitle = dialogtitle; // 标题
  164. ofn2.ulFlags = 0x00000040; // 新的样式,带编辑框
  165. IntPtr pidlPtr = SHBrowseForFolder(ofn2);
  166. char[] charArray = new char[2048];
  167. for (int i = 0; i < 2048; i++)
  168. {
  169. charArray[i] = '\0';
  170. }
  171. SHGetPathFromIDList(pidlPtr, charArray);
  172. string fullDirPath = new string(charArray);
  173. fullDirPath = fullDirPath.Substring(0, fullDirPath.IndexOf('\0'));
  174. return fullDirPath;
  175. }
  176. catch (Exception e)
  177. {
  178. Debug.LogError(e);
  179. }
  180. return string.Empty;
  181. }
  182. /// <summary>
  183. /// 选择文件
  184. /// </summary>
  185. /// <param name="title"></param>
  186. /// <param name="directory"></param>
  187. /// <param name="filterName"></param>
  188. /// <param name="filterExtensions"></param>
  189. /// <returns></returns>
  190. public static string[] SelectFile(string title,string directory,string filterName, params string[] filterExtensions)
  191. {
  192. List<ExtensionFilter> extensions = new List<ExtensionFilter>();
  193. extensions.Add(new ExtensionFilter(filterName, filterExtensions));
  194. return StandaloneFileBrowser.OpenFilePanel(title,directory, extensions.ToArray(),false);
  195. }
  196. /// <summary>
  197. /// 多选文件
  198. /// </summary>
  199. /// <param name="filterName"></param>
  200. /// <param name="mutiltSelect"></param>
  201. /// <param name="filterExtensions"></param>
  202. /// <returns></returns>
  203. public static List<string> OpenProject(string filterName, bool mutiltSelect, params string[] filterExtensions)
  204. {
  205. List<string> list = new List<string>();
  206. List<ExtensionFilter> extensions = new List<ExtensionFilter>();
  207. extensions.Add(new ExtensionFilter(filterName, filterExtensions));
  208. string[] paths = StandaloneFileBrowser.OpenFilePanel(" 上传文件", "", extensions.ToArray(), mutiltSelect);
  209. foreach (string Wenjian in paths)
  210. {
  211. //路径
  212. string pathName = Path.GetDirectoryName(Wenjian);
  213. //带扩展名的的指定路径文件名
  214. string fileName = Path.GetFileName(Wenjian);
  215. list.Add(pathName + "/" + fileName);
  216. }
  217. return list;
  218. }
  219. #endregion
  220. }
  221. #endregion