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.filter = "图片文件(*.jpg*.png)\0*.jpg;*.png";
        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);//返回路径的最后一个文件夹名称
           }
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 );
}

在这里插入图片描述

原文:https://blog.csdn.net/qq_38834505/article/details/99826830

补充:
OpenFileDialog对话框的Filter属性说明:

首先说明一个示例,分析一下Filter属性的构成:“ Excel文件|.xls ”,前面的“Excel文件”成为标签,是一个可读的字符串,可以自定定义,“|.xls”是筛选器,表示筛选文件夹中后缀名为.xls的文件,“*”表示匹配Excel文件名称的字符串。

OK,下面说说我们经常要用到的几种情况:

1. Filter是null或Empty,表示显示所有文件,并总会显示文件夹

2. 需要筛选特定的文件,设置Filter属性为“标签|*.后缀”,按照这个格式设置,标签可以自定义,是字符串即可,后缀表示你需要筛选的文件后缀,例如“.txt、.doc”等

3. 需要筛选多种文件,比方说需要筛选图片文件,但是图片文件的后缀有几种,例如jpg、png、gif等,当需要同时筛选这些文件,设置Filter属性为“标签|.jpg;.png;*.gif”,注意:只是在筛选器中多添加了几个后缀,不同后缀之间使用分号隔开

4.当需要筛选多种文件,但是不同时将它们全部列出来,只有用户分别通过下拉列表选择需要的文件类型时,才进行筛选。这种情况下只需要多设置几个筛选器即可,filter属性设置如下:“标签1|*.jpg|标签2|.png|标签3|.gif”。注意:不同的筛选器之间使用“|”分隔即可。

Filter属性类似与正则表达式,试用*表示匹配文件名的字符,使用“.后缀”匹配文件的后缀名,通过连接后缀(试用;号将需要的后缀分开)表示同时筛选所有的符合后缀的文件,通过“|”连接不同的筛选器表示通过用户选择后缀名称来进行文件筛选

private void btnupload_Click(object sender, EventArgs e)
        {
    
    
            using (OpenFileDialog open = new OpenFileDialog())
            {
    
    
                open.Multiselect = false;
                open.Title = "打开文件";
                open.Filter = "图片|*.jpg;*.png;*.gif;*.jpeg;*.bmp";
                if (open.ShowDialog() == DialogResult.OK)
                {
    
    
                                   
                }
            }
        }

猜你喜欢

转载自blog.csdn.net/weixin_45023328/article/details/125764951