文件另存、打开

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

public class SelectFileLog : MonoBehaviour
{
    #region 选择路径
    [DllImport("shell32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    public static extern IntPtr SHBrowseForFolder([In, Out] SelectFile ofn);

    [DllImport("shell32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    public static extern bool SHGetPathFromIDList([In] IntPtr pidl, [In, Out] char[] fileName);

    public static string GetSelectFileName()//选择路径
    {
        SelectFile selectFileName = new SelectFile();
        selectFileName.pszDisplayName = new string(new char[2000]);
        selectFileName.lpszTitle = "选择文件夹";
        IntPtr intPtr = SHBrowseForFolder(selectFileName);
        char[] chArray = new char[2000];
        for (int i = 0; i < chArray.Length; i++)
        {
            chArray[i] = '\0';
        }
        SHGetPathFromIDList(intPtr, chArray);
        string fullPath = new string(chArray);
        fullPath = fullPath.Substring(0, fullPath.IndexOf('\0'));
        return fullPath;
    }

    public static string selectPath;
    public static string SeleclFile()
    {
        Debug.LogError("选择的路径:" + selectPath);
        return selectPath = SelectFileLog.GetSelectFileName();
    }
    #endregion

    #region 打开路径
    [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    public static extern bool GetOpenFileName([In, Out] OpenFileName ofn);

    public static bool openFileName([In, Out] OpenFileName ofn)
    {
        return GetOpenFileName(ofn);
    }
    public static OpenFileName OpenFileName(FlieType flieType)
    {
        OpenFileName openFile = new OpenFileName();
        openFile.structSize = Marshal.SizeOf(openFile);

        openFile.filter = GetFileType(flieType);
        openFile.file = new string(new char[256]);
        openFile.maxFile = openFile.file.Length;
        openFile.fileTitle = new string(new char[64]);
        openFile.maxFileTitle = openFile.fileTitle.Length;
        //这里设置默认路径
        string path = Application.streamingAssetsPath + "/JsonData";
        path = path.Replace('/', '\\');
        openFile.initialDir = path;
        openFile.title = "打开文件夹";
        openFile.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;
        return openFile;
        #region
        //if (GetOpenFileName(openFile))
        //{
        //    FileStream fileStream = new FileStream(openFile.file, FileMode.Open, FileAccess.Read);
        //    fileStream.Seek(0, SeekOrigin.Begin);
        //    //创建文件长度的缓冲区
        //    byte[] bytes = new byte[fileStream.Length];
        //    //读取文件
        //    fileStream.Read(bytes, 0, (int)fileStream.Length);
        //    fileStream.Close();
        //    fileStream.Dispose();
        //    fileStream = null;

        //    //创建Texture           
        //    Texture2D texture2D = new Texture2D(100, 100);
        //    texture2D.LoadImage(bytes);
        //    RawImage image = GameObject.Find("image").GetComponent<RawImage>();
        //    image.gameObject.SetActive(true);
        //    if (image == null)
        //    {
        //        Debug.LogError("查找RawImages失败");
        //        return;
        //    }
        //    image.texture = texture2D;
        //    image.SetNativeSize();
        //}
        #endregion
    }
    #endregion

    #region 路径保存文件
    //另存为对话框
    [DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
    public static extern bool GetSaveFileName([In, Out] OpenFileName ofn);
    public static bool SaveFileName([In, Out] OpenFileName ofn)
    {
        return GetSaveFileName(ofn);
    }
    public static OpenFileName SaveFileName()
    {
        OpenFileName openFile = new OpenFileName();
        openFile.structSize = Marshal.SizeOf(openFile);
        openFile.filter = "txt文件(*.txt)\0*.txt";
        openFile.file = new string(new char[256]);
        openFile.maxFile = openFile.file.Length;
        openFile.fileTitle = new string(new char[64]);
        openFile.maxFileTitle = openFile.fileTitle.Length;

        //这里设置默认路径
        string path = Application.streamingAssetsPath + "/JsonData";
        path = path.Replace('/', '\\');
        openFile.initialDir = path;
        openFile.title = "保存本地文件";
        openFile.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;

        //if (GetSaveFileName(openFile))
        //{
        //    FileInfo file = new FileInfo(openFile.file);
        //    StreamWriter sw = file.CreateText();
        //    sw.Write(openFile.file);
        //    sw.Dispose();
        //    sw.Close();
        //}
        return openFile;
    }
    #endregion

    public static string GetFileType(FlieType flieType)
    {
        switch (flieType)
        {
            case FlieType.txt:
                return "txt文件(*.txt)\0*.txt";

            case FlieType.mp3:
                return "mp3文件(*.mp3*.wav)\0*.mp3;*.wav";

            case FlieType.mp4:
                return "mp4文件(*.mp4)\0*.mp4";

            case FlieType.image:
                return "图片文件(*.jpg*.png)\0*.jpg;*.png";

            default: return null;
        }
    }
}


[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class SelectFile
{
    public IntPtr hwndOwner = IntPtr.Zero;
    public IntPtr pidlRoot = IntPtr.Zero;
    public String pszDisplayName = null;
    public String lpszTitle = null;
    public UInt32 ulFlags = 0;
    public IntPtr lpfn = IntPtr.Zero;
    public IntPtr lParam = IntPtr.Zero;
    public int iImage = 0;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class OpenFileName
{
    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 = ".txt";
    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 enum FlieType
{
    txt,
    mp3,
    mp4,
    image
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值