using System.IO; using System.Diagnostics; using System; using System.Timers; namespace Uninpho.Tools { public class Utility { /// /// 在指定时间过后执行指定的表达式 /// /// 事件之间经过的时间(以毫秒为单位) /// 要执行的表达式 public static void SetTimeout(double interval, Action action) { System.Timers.Timer timer = new System.Timers.Timer(interval); timer.Elapsed += delegate(object sender, System.Timers.ElapsedEventArgs e) { timer.Enabled = false; action(); }; timer.Enabled = true; } /// /// 在指定时间周期重复执行指定的表达式 /// /// 事件之间经过的时间(以毫秒为单位) /// 要执行的表达式 public static void SetInterval(double interval, Action action) { System.Timers.Timer timer = new System.Timers.Timer(interval); timer.Elapsed += delegate(object sender, System.Timers.ElapsedEventArgs e) { action(e); }; timer.Enabled = true; } /// /// 获取配置文件的路径 /// /// public static string GetTemPath(string dbPath) { string temPath = Path.Combine(Path.GetDirectoryName(dbPath),"temp"); if (!Directory.Exists(temPath)) { Directory.CreateDirectory(temPath); File.SetAttributes(temPath, FileAttributes.Hidden | FileAttributes.System); } return temPath; } /// /// 获取Assembly的运行路径 /// /// public static string GetAssemblyPath() { string _CodeBase = System.Reflection.Assembly.GetExecutingAssembly().CodeBase; _CodeBase = _CodeBase.Substring(8, _CodeBase.Length - 8); // 8是file:// 的长度 string[] arrSection = _CodeBase.Split(new char[] { '/' }); string _FolderPath = ""; for (int i = 0; i < arrSection.Length - 1; i++) { _FolderPath += arrSection[i] + "/"; } return _FolderPath; } /// /// 获取发布的数据路径 /// /// public static string GetDataPath() { string tileDataDir = ""; string _CodeBase = GetAssemblyPath(); tileDataDir = System.IO.Path.Combine(System.IO.Directory.GetParent(_CodeBase).Parent.FullName, "data"); if (!System.IO.Directory.Exists(tileDataDir)) Directory.CreateDirectory(tileDataDir); return tileDataDir; } /// /// 调用外部Exe /// /// /// public static Process CallExe(string exePath, string parameters) { Process process = new System.Diagnostics.Process(); process.StartInfo.FileName = exePath; process.StartInfo.Arguments = parameters; process.StartInfo.UseShellExecute = false; process.StartInfo.CreateNoWindow = true; process.StartInfo.RedirectStandardOutput = false; process.EnableRaisingEvents = true; //process.Start(); return process; } /// /// 调用外部Exe /// /// /// public static string[] GetFiles(string dir, string filter,SearchOption option = SearchOption.TopDirectoryOnly) { return Directory.GetFiles(dir, filter, option); } } }