config.ini存放在Assets目录下,项目打包之后存放在Data目录下exe同目录

// 从配置文件读取
string configFile = Application.dataPath + "/config.ini";//打包好的“xxx_Data”目录貌似没有读取里面的文件权限
//所以对于打包的程序,需要把配置文件config.ini放在exe同目录下
#if !UNITY_EDITOR
configFile = System.Environment.CurrentDirectory + "/config.ini";
#endif
if (File.Exists(configFile))
{
ConfigIni ini = new ConfigIni(configFile);
//host = ini.ReadIniContent("DeepStreamServer", "host");
//port = ini.ReadIniContent("DeepStreamServer", "port");
host = ini.keyVal["host"];
port = ini.keyVal["port"];
//MessageBox(IntPtr.Zero, configFile+","+host+","+port, "从配置文件读取DeepStream的IP:Port", 0);
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using UnityEngine; /// <summary>
/// 读取ini配置文件
/// [Time]
/// time=10
/// [Speed]
/// speed=5
/// ConfigIni ini=new ConfigIni(Application.StreamingAssets+"/Setting.ini");
/// time=ini.ReadIniContent("Time","time");
/// speed=ini.ReadIniContent("Speed","speed");
/// ini.WritePrivateProfileString("Count","count","5");
/// </summary>
public class ConfigIni { public string path;
public Dictionary<string, string> keyVal = new Dictionary<string, string>(); //ini文件的路径
public ConfigIni(string path)
{
this.path = path; StreamReader sr = new StreamReader(path, Encoding.Default);
string line;
while ((line = sr.ReadLine()) != null)
{
if (line.Contains("="))
{
string[] kv = line.Split('=');
string key = kv[].Trim();
string v = kv[].Trim();
keyVal.Add(key, v);
}
}
} [DllImport("kernel32")]
public static extern long WritePrivateProfileString(string section, string key, string value, string path);
[DllImport("kernel32")]
public static extern int GetPrivateProfileString(string section, string key, string deval, StringBuilder stringBuilder, int size, string path);
[DllImport("User32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
public static extern int MessageBox(IntPtr handle, String message, String title, int type); //写入ini文件
public void WriteIniContent(string section, string key, string value)
{
WritePrivateProfileString(section, key, value, this.path);
} //读取Ini文件
public string ReadIniContent(string section, string key)
{
StringBuilder temp = new StringBuilder();
int i = GetPrivateProfileString(section, key, "", temp, , this.path);
//MessageBox(IntPtr.Zero, this.path+i + ","+temp+","+section+key, "ReadIniContent", 0);
return temp.ToString();
}
//判断路径是否正确
public bool IsIniPath()
{
return File.Exists(this.path);
}
}

最新文章

  1. 自定义JS常用方法
  2. windows 上执行python pywin32.exe
  3. PHP连接SQLServer
  4. ASP.NET MVC学习笔记-----ActionInvoker
  5. Java for LeetCode 150 Evaluate Reverse Polish Notation
  6. photoshop:多边形选项
  7. mvc5入门,经典教程。。
  8. XAML 名称范围 (x:) 语言特性
  9. Android 框架炼成 教你怎样写组件间通信框架EventBus
  10. 职责链模式实例(C#)
  11. webpack 3.X学习之图片处理
  12. RTMPdump(libRTMP) 源代码分析 10: 处理各种消息(Message)
  13. 【Mac】-NO.100.Mac.1.java.1.001-【Mac Install multiple JDK】-
  14. Django view 视图
  15. java使用何种类型表示精确的小数?
  16. python全栈 字典数据类型相关知识及操作
  17. 深入理解 Java 内存模型(一)- 内存模型介绍
  18. Sencha Touch+PhoneGap打造超级奶爸之喂养记(一) 源码免费提供(转)
  19. Spring源码分析(九)解析默认标签中的自定义标签元素
  20. 设计模式(Python)-观察者模式

热门文章

  1. linux中怎么查看ip地址
  2. 电源PI相关知识讲解
  3. Spring加载静态资源的方式
  4. 101 个 MySQL 的调节和优化的提示
  5. SAP+ 差旅报销集成方案的实现
  6. C#基础--------------------C#正则表达式
  7. unity执行顺序问题(如何再次执行start方法)
  8. 高性能分布式哈希表FastDHT
  9. QTreeView
  10. kubectl error: The connection to the server localhost:8080 was refused