Unity對資源管理器操作 打開資源管理器選擇文件並篩選文件

打開資源管理器

Application.OpenURL(path);

打開選擇文件  篩選文件類型(只要type類型的文件顯示)

 public void Openfile(string type)
    {

        OpenFileDlg pth = new OpenFileDlg();
        pth.structSize = System.Runtime.InteropServices.Marshal.SizeOf(pth);

        pth.filter = "文件(*." + type + ")\0*." + type + "";//篩選文件類型
        pth.file = new string(new char[256]);
        pth.maxFile = pth.file.Length;
        pth.fileTitle = new string(new char[64]);
        pth.maxFileTitle = pth.fileTitle.Length;
        pth.initialDir = Application.streamingAssetsPath.Replace('/', '\\');  // default path
        pth.title = "選擇項目json";
        pth.defExt = "JPG";//顯示文件類型
        pth.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
        if (OpenFileDialog.GetOpenFileName(pth))
        {
            string filepath = pth.file;//選擇的文件路徑;

            DirectoryInfo i = new DirectoryInfo(filepath);

            //上級目錄
            string path = i.Parent.FullName;//返回文件的上級目錄

            ProjectData openprodata = new ProjectData();
            openprodata.proname = Path.GetFileNameWithoutExtension(path);//返回路徑的最後一個文件夾名稱
           }

調用的是openfileDgl      搬走不謝

using System;
using System.Runtime.InteropServices;
 
[ StructLayout( LayoutKind.Sequential, CharSet=CharSet.Auto )]  
 
public class OpenFileDlg
{
    public int      structSize = 0;
    public IntPtr   dlgOwner = IntPtr.Zero; 
    public IntPtr   instance = IntPtr.Zero;
    public String   filter = null;
    public String   customFilter = null;
    public int      maxCustFilter = 0;
    public int      filterIndex = 0;
    public String   file = null;
    public int      maxFile = 0;
    public String   fileTitle = null;
    public int      maxFileTitle = 0;
    public String   initialDir = null;
    public String   title = null;   
    public int      flags = 0; 
    public short    fileOffset = 0;
    public short    fileExtension = 0;
    public String   defExt = null; 
    public IntPtr   custData = IntPtr.Zero;  
    public IntPtr   hook = IntPtr.Zero;  
    public String   templateName = null; 
    public IntPtr   reservedPtr = IntPtr.Zero; 
    public int      reservedInt = 0;
    public int      flagsEx = 0;
}
 
public class OpenFileDialog
{
	[DllImport("Comdlg32.dll",SetLastError=true,ThrowOnUnmappableChar=true, CharSet = CharSet.Auto)]          
	public static extern bool GetOpenFileName([ In, Out ] OpenFileDlg ofd );
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章