using System;
using System.IO;
using System.Runtime.InteropServices;
using UnityEngine;
//脚本FileManager
public static class FileManager
{
public static void OpenFile(string type, Action
{
OpenFileDlg pth = new OpenFileDlg();
pth.structSize = Marshal.SizeOf(pth);
pth.filter = 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.dataPath.Replace('/', '\\'); // default path
pth.title = "选择文件";
pth.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
if (OpenFileDialog.GetOpenFileName(pth))
{
//string filepath = pth.file;//选择的文件路径;
FileInfo fileInfo = new FileInfo(pth.file);
//DirectoryInfo i = new DirectoryInfo(filepath);
上级目录
//string path = i.Parent.FullName;//返回文件的上级目录
if (loaded != null)
{
loaded(pth.file);
}
}
}
static string oldPatn = "";
public static void OpenFile(string type, Action
{
OpenFileDlg pth = new OpenFileDlg();
pth.structSize = Marshal.SizeOf(pth);
pth.filter = type;//筛选文件类型
//pth.filter = "图片文件(*.jpg*.png)\0*.jpg;*.png";
pth.file = new string(new char[1024]);
pth.maxFile = pth.file.Length;
pth.fileTitle = new string(new char[64]);
pth.maxFileTitle = pth.fileTitle.Length;
if(oldPatn != "")
{
pth.initialDir = oldPatn; // default path
}
else
{
pth.initialDir = Application.dataPath.Replace('/', '\\'); // default path
}
pth.title = "选择文件";
pth.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
if (OpenFileDialog.GetOpenFileName(pth))
{
oldPatn = pth.file;
//string filepath = pth.file;//选择的文件路径;
string[] SplitStr = { "\0" };
string[] strs = pth.file.Split(SplitStr, StringSplitOptions.RemoveEmptyEntries);
FileInfo fileInfo = null;
FileStream fsRead = null;
byte[] byteArrayRead = null;
if (strs.Length > 1)
{
for (int i = 1; i < strs.Length; i++)
{
Call(strs[0]+"\\"+strs[i], fileInfo, fsRead, byteArrayRead, loaded);
}
}
else
{
Call(strs[0], fileInfo, fsRead, byteArrayRead, loaded);
}
}
}
private static void Call(string filePath,FileInfo fileInfo, FileStream fsRead, byte[] byteArrayRead,Action
{
fileInfo = new FileInfo(filePath);
fsRead = new FileStream(filePath, FileMode.Open);
byteArrayRead = new byte[fileInfo.Length]; // 1字节*1024 = 1k 1k*1024 = 1M内存
//通过死缓存去读文本中的内容
while (true)
{
//readCount 这个是保存真正读取到的字节数
int readCount = fsRead.Read(byteArrayRead, 0, byteArrayRead.Length);
开始写入读取到缓存内存中的数据到目标文本文件中
//fsRead.Write(byteArrayRead, 0, readCount);
if (readCount < byteArrayRead.Length)
{
break; //结束循环
}
}
fsRead.Close();
fsRead.Dispose();
//DirectoryInfo i = new DirectoryInfo(filepath);
FileData fileData = new FileData();
fileData.DirectoryName = fileInfo.DirectoryName;
fileData.Length = fileInfo.Length;
fileData.Name = fileInfo.Name;
fileData.bytes = byteArrayRead;
fileData.bytesLength = byteArrayRead.Length;
fileData.filePath = filePath;
上级目录
//string path = i.Parent.FullName;//返回文件的上级目录
if (loaded != null)
{
loaded(fileData);
}
}
}