/// <summary> /// 初始化数据 /// </summary> private void Init() { try { // 初始化sql连接 string fileName = Path.Combine(Application.StartupPath, "AutoExport.ini"); string content = FileFolderHelper.FileToString(fileName); IniStructure m_inis = new IniStructure(); m_inis = IniStructure.ReadIniWithContent(content); string connectString = m_inis.GetValue("General", "ConnString"); conn = new SqlConnection(connectString); // 读取根目录路径 rootPath = m_inis.GetValue("General", "RootDir"); // 读取导出表列表 string[] autoTableArray = m_inis.GetKeys("AutoExport"); autoTableList.AddRange(autoTableArray); string[] customTableArray = m_inis.GetKeys("CustomExport"); customTableList.AddRange(customTableArray); // 读取资源文件列表 string[] fileArray = m_inis.GetKeys("Resource"); fileList.AddRange(fileArray); // 读取自动导出表的配置信息 string sqlString = string.Format("SELECT * FROM sys_export_table_cfg"); configTable = GetDataTable(sqlString); // 更新资源文件 DownLoadResource(); // 初始化lua虚拟机 exportLua = new Lua(); exportLua["Conn"] = conn; exportLua["RootDir"] = rootPath; string luaFile = Path.Combine(Application.StartupPath, "export.lua"); exportLua.DoFile(luaFile); postExportLua = new Lua(); postExportLua["Conn"] = conn; postExportLua["RootDir"] = rootPath; postExportLua["___GIsServer"] = true; luaFile = Path.Combine(Application.StartupPath, "post_export.lua"); postExportLua.DoFile(luaFile); } catch (Exception ex) { logText.Append(string.Format("{0} —— 产生异常:{1}\r\n", DateTime.Now, ex.Message)); } }
/// <summary> /// 初始化数据 /// </summary> private bool Init() { try { outputDebugString(string.Format("{0} —— 开始Init初始化...", DateTime.Now)); outputDebugString(string.Format("{0} —— 正在初始化sql连接...", DateTime.Now)); // 初始化sql连接 string fileName = Path.Combine(Application.StartupPath, "AutoExport.ini"); string content = FileFolderHelper.FileToString(fileName); IniStructure m_inis = new IniStructure(); m_inis = IniStructure.ReadIniWithContent(content); string connectString = m_inis.GetValue("General", "ConnString"); conn = new SqlConnection(connectString); // 读取根目录路径 outputDebugString(string.Format("{0} —— 正在初始化外部设置...", DateTime.Now)); rootPath = m_inis.GetValue("General", "RootDir"); // 读取导出表列表 string[] autoTableArray = m_inis.GetKeys("AutoExport"); autoTableList.AddRange(autoTableArray); string[] customTableArray = m_inis.GetKeys("CustomExport"); customTableList.AddRange(customTableArray); outputDebugString(string.Format("{0} —— 正在更新资源文件...", DateTime.Now)); // 读取资源文件列表 string[] fileArray = m_inis.GetKeys("Resource"); fileList.AddRange(fileArray); // 读取自动导出表的配置信息 string sqlString = string.Format("SELECT * FROM sys_export_table_cfg"); configTable = GetDataTable(sqlString); // 更新资源文件 if (Program.GSTEP != 2) { DownLoadResource(); } outputDebugString(string.Format("{0} —— 正在设置path...", DateTime.Now)); // path更新 EV ev = new EV(); ev.evPath(Path.GetDirectoryName(Application.ExecutablePath)); outputDebugString(string.Format("{0} —— 正在初始化lua虚拟机...", DateTime.Now)); // 初始化lua虚拟机 exportLua = new Lua(); exportLua["Conn"] = conn; exportLua["RootDir"] = rootPath; string luaFile = Path.Combine(Application.StartupPath, "export.lua"); exportLua.DoFile(luaFile); postExportLua = new Lua(); postExportLua["___GIsServer"] = true; postExportLua["RootDir"] = rootPath; postExportLua["Conn"] = conn; postExportLua.RegisterFunction("GetDataTableRow", this, typeof(ExportManager).GetMethod("GetDataTableRow")); luaFile = Path.Combine(Application.StartupPath, "post_export.lua"); postExportLua.DoFile(luaFile); outputDebugString(string.Format("{0} —— 完成所有初始化工作!", DateTime.Now)); return(true); } catch (Exception ex) { outputDebugStringError(string.Format("{0} —— 初始化init产生异常:{1}", DateTime.Now, ex.Message)); return(false); } }
/// <summary> /// 比较并下载资源文件 /// </summary> private void DownLoadResource() { foreach (string s in fileList) { FtpWebRequest ftp = FtpWebRequest.Create(new Uri(s)) as FtpWebRequest; ftp.UseBinary = true; ftp.KeepAlive = false; string strConfig = FileFolderHelper.FileToString("./autoexport.ini"); if (strConfig.Length == 0) { outputDebugStringError(string.Format("{0} —— 读取ini时产生异常", DateTime.Now)); } bool bConfigUseIP = false; if (strConfig.IndexOf("changan") == -1) { bConfigUseIP = true; } string localFileName = s.Replace(bConfigUseIP ? "ftp://192.168.27.12/newtools/" : "ftp://changan/newtools/", ""); localFileName = Path.Combine(Application.StartupPath, localFileName); FileInfo fi = new FileInfo(localFileName); if (fi.Exists) // 本地文件已经存在 { ftp.Method = WebRequestMethods.Ftp.GetDateTimestamp; FtpWebResponse response = ftp.GetResponse() as FtpWebResponse; DateTime newEditTime = response.LastModified; response.Close(); DateTime localEditTime = fi.LastWriteTime; if (newEditTime > localEditTime) // 需要更新文件 { fi.Delete(); ftp.Method = WebRequestMethods.Ftp.DownloadFile; response = (FtpWebResponse)ftp.GetResponse(); Stream ftpStream = response.GetResponseStream(); long contentLength = response.ContentLength; int bufferSize = 2048; int readCount; byte[] buffer = new byte[bufferSize]; readCount = ftpStream.Read(buffer, 0, bufferSize); FileStream fileStream = new FileStream(localFileName, FileMode.Create); while (readCount > 0) { fileStream.Write(buffer, 0, readCount); readCount = ftpStream.Read(buffer, 0, bufferSize); } ftpStream.Close(); fileStream.Close(); response.Close(); } } else { ftp.Method = WebRequestMethods.Ftp.DownloadFile; FtpWebResponse response = (FtpWebResponse)ftp.GetResponse(); Stream ftpStream = response.GetResponseStream(); long contentLength = response.ContentLength; int bufferSize = 2048; int readCount; byte[] buffer = new byte[bufferSize]; readCount = ftpStream.Read(buffer, 0, bufferSize); string folderName = Path.GetDirectoryName(localFileName); // 获取目录 if (!Directory.Exists(folderName)) // 目录不存在 { Directory.CreateDirectory(folderName); // 先创建目录,再创建文件 } FileStream fileStream = new FileStream(localFileName, FileMode.Create); while (readCount > 0) { fileStream.Write(buffer, 0, readCount); readCount = ftpStream.Read(buffer, 0, bufferSize); } ftpStream.Close(); fileStream.Close(); response.Close(); } } }