当前位置:网站首页>Unity对资源管理器操作 打开资源管理器选择文件并筛选文件
Unity对资源管理器操作 打开资源管理器选择文件并筛选文件
2022-07-26 21:16:00 【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)
{
}
}
}
边栏推荐
- 京东一面:Redis 如何实现库存扣减操作?如何防止商品被超卖?
- day07-
- Props with type Object/Array must...
- 带你搞懂MySQL隔离级别,两个事务同时操作同一行数据会怎样?
- 进程的概念和分类
- Operating guidelines and suggestions for spot gold (Part 1)
- C# 数据类型_摘自菜鸟教程
- VB.net Chart1的处理
- Oppo self-developed large-scale knowledge map and its application in digital intelligence engineering
- In depth analysis of the source code, why is the string class immutable? (hit me before you understand)
猜你喜欢
随机推荐
matlab 短时自相关实现
MOS 管示意图
Basic operation of (C language) files
虾皮shopee根据关键词取商品列表 API
Pytorch squeeze() unsqueeze() 用法
JS 延迟执行window.onload
What to do if the browser home page is tampered with, and how to recover if the home page is tampered with
攻防世界----ics-07
Tester: "I have five years of testing experience" HR: "no, you just used one year of work experience for five years."
06 CP command
Matlab pitch period estimation post-processing
Props with type Object/Array must...
Understanding and practice of the trend of Bank of London foreign exchange
Ansible installation and use
Altium Designer 22 修改选中元件的层属性
[audio and video] ijkplayer player parameter description document
imshow()函数后面如果不加waitKey()函数就不显示
07 DF command
七月集训(第26天) —— 并查集
深入源码剖析String类为什么不可变?(还不明白就来打我)




![[shutter -- geTx] pop up - dialog, snackBar, bottomsheet](/img/17/af2e45620e96a78235081145b7bb76.png)


![[MySql]substr用法-查询表的某个字段的具体位数的值](/img/d5/68658ff15f204dc97abfe7c9e6b354.png)
