using System; using System.Diagnostics; using System.IO; using System.Net; namespace OS.Spin.Common { /// /// 文件 操作 /// public static class FilesOperate { /// /// 获取App的当前路径 \\结束 /// /// public static string getAppPath() { return GetAssemblyPath(); } /// /// 获取Assembly的运行路径 \\结束 /// /// public static string GetAssemblyPath() { try { string sCodeBase = System.Reflection.Assembly.GetExecutingAssembly().CodeBase; sCodeBase = sCodeBase.Substring(8, sCodeBase.Length - 8); // 8是 file:// 的长度 string[] arrSection = sCodeBase.Split(new char[] { '/' }); string sDirPath = ""; for (int i = 0; i < arrSection.Length - 1; i++) { sDirPath += arrSection[i] + Path.DirectorySeparatorChar; } return sDirPath; } catch (Exception ex) { LogisTrac.WriteLog(string.Format("GetAssemblyPath:{0}", ex.Message)); return ""; } } /// /// 文件夹复制 /// /// 原始路径 /// 目标路径 /// public static bool CopyDirectory(string sSourceDirName, string sDestDirName) { try { if (string.IsNullOrEmpty(sSourceDirName) || string.IsNullOrEmpty(sDestDirName)) { return false; } //不复制.svn文件夹 if (sSourceDirName.EndsWith("svn")) { return true; } if (sSourceDirName.Substring(sSourceDirName.Length - 1) != Path.DirectorySeparatorChar.ToString()) { sSourceDirName = sSourceDirName + Path.DirectorySeparatorChar; } if (sDestDirName.Substring(sDestDirName.Length - 1) != Path.DirectorySeparatorChar.ToString()) { sDestDirName = sDestDirName + Path.DirectorySeparatorChar; } #region 复制函数 if (Directory.Exists(sSourceDirName)) { if (!Directory.Exists(sDestDirName)) { Directory.CreateDirectory(sDestDirName); } foreach (string item in Directory.GetFiles(sSourceDirName)) { File.Copy(item, sDestDirName + System.IO.Path.GetFileName(item), true); } foreach (string item in Directory.GetDirectories(sSourceDirName)) { CopyDirectory(item, sDestDirName + item.Substring(item.LastIndexOf(Path.DirectorySeparatorChar) + 1)); } } return true; #endregion } catch (Exception ex) { LogisTrac.WriteLog(string.Format("CopyDirectory:{0}", ex.Message)); return false; } } /// /// 启动其他的应用程序 /// /// 应用程序名称 /// 应用程序工作目录 /// 命令行参数 /// 窗口风格 public static bool StartProcess(string file, string workdirectory, string args, ProcessWindowStyle style) { try { Process pMyProcess = new Process(); ProcessStartInfo pStartInfo = new ProcessStartInfo(file, args); pStartInfo.WindowStyle = style; pStartInfo.WorkingDirectory = workdirectory; pMyProcess.StartInfo = pStartInfo; pMyProcess.StartInfo.UseShellExecute = false; pMyProcess.Start(); return true; } catch (Exception ex) { LogisTrac.WriteLog(string.Format("StartProcess:{0}", ex.Message)); //LogAPI.debug(ex); return false; } } /// /// 获得本地计算机名 /// /// public static string GetComputerName() { return Dns.GetHostName(); } /// /// 获得计算机IP地址 /// /// public static string GetIPAddress() { try { string sComputerName; sComputerName = GetComputerName(); string sIpAddress = ""; IPAddress[] addr = Dns.GetHostAddresses(sComputerName); //for (int i = 0; i < addr.Length; i++) //{ // sIpAddress += addr[i].ToString() + " "; //} sIpAddress = addr[0].ToString(); return sIpAddress; } catch (Exception ep) { //LogAPI.debug(ep); return "127.0.0.1"; } } /// /// 描述:创建目录 /// /// public static bool CreateFolder(string sFolder) { //如果临时文件夹不存在,则创建该文件夹 if (!Directory.Exists(sFolder)) { Directory.CreateDirectory(sFolder); } return true; } } }