///######################################################################### /// <summary> /// 创建文件 /// coder:zhangjiechen(2010-09-25) /// </summary> /// <param name="objData">要创建文件的内容</param> /// <param name="sPath">文件的路径</param> /// <param name="sFileName">文件的名字</param> /// <returns>true 成功 false 失败</returns> public static bool CreateFile(object objData, string sDirPath, string sFileName) { try { if (objData == null) { return(false); } if (!Directory.Exists(sDirPath)) { Directory.CreateDirectory(sDirPath); } byte[] bytes = null; if (objData.GetType() == typeof(String)) { bytes = System.Text.Encoding.Default.GetBytes((string)objData); } else if (objData.GetType() == typeof(byte[])) { bytes = (byte[])objData; } FileStream fileStream = new FileStream(sDirPath + "\\" + sFileName, FileMode.Create); fileStream.Write(bytes, 0, bytes.Length); fileStream.Close(); } catch (Exception ex) { ExManager.ReportException(ex); return(false); } return(true); }
/// ############################################################### /// <summary> /// 通过相隔日期天数计算Between表达式的两个日期,即N天之前/后。只要传入相隔天数和一个日期即可,另外一个日期参数赋值为DateTime.MinValue /// Coder: Sum(20120806) /// </summary> /// <param name="nDayCount">相隔天数(正数,当天传0)</param> /// <param name="dtBeginDate">开始时间,赋值即为计算N天之后的两个日期</param> /// <param name="dtEndDate">结束时间,赋值即为计算N天之前的两个日期</param> /// <returns>是否成功转换</returns> public static bool GetBetweenDateByDayCount(int nDayCount, ref DateTime dtBeginDate, ref DateTime dtEndDate) { try { if (nDayCount < 0) { return(false); } if (dtBeginDate != DateTime.MinValue) { dtBeginDate = dtBeginDate.Date; dtEndDate = nDayCount == 0 ? dtBeginDate.Date.AddDays(nDayCount + 1).AddSeconds(-1) : dtBeginDate.Date.AddDays(nDayCount).AddSeconds(-1); return(true); } if (dtEndDate != DateTime.MinValue) { dtEndDate = nDayCount == 0 ? dtBeginDate.Date.AddDays(nDayCount + 1).AddSeconds(-1) : dtBeginDate.Date.AddDays(nDayCount).AddSeconds(-1); dtBeginDate = dtEndDate.Date; return(true); } return(false); } catch (Exception ex) { ExManager.ReportException(ex); return(false); } }
/// ################################################## /// <summary> /// 获取服务器时间 /// coder:Jone(2010-04-22) /// </summary> /// <returns></returns> public static DateTime GetServerDateTime() { //XPO获取服务器时间的方法 try { using (UnitOfWork _theUow = new UnitOfWork()) { return((DateTime)_theUow.Evaluate(typeof(XPObjectType), new FunctionOperator(FunctionOperatorType.Now), null));// 英文版的时候出现过问题,暂时不用,还是用SQL } } catch (Exception exception) { ExManager.ReportException(exception); //ConnectionPool pool = new ConnectionPool(); //using (SqlConnection conn = pool.GetConnection()) //{ // try // { // SqlCommand cmd = new SqlCommand("SELECT GETDATE()", conn); // return (DateTime)cmd.ExecuteScalar(); // } // catch (Exception ex) // { // ExManager.ReportException(ex); // return DateTime.Now; // } // finally // { // if (conn != null && conn.State == System.Data.ConnectionState.Open) // conn.Close(); // } //} return(DateTime.Now); } }
/// ################################################################# /// <summary> /// 从模块的列配置文件(xml文件)中读入列配置 /// Coder: Gary(2010-06-28) /// </summary> /// <param name="moduleName"></param> /// <returns></returns> public static IList <ListColumn> GetAvailableColumns(string moduleName) { IList <ListColumn> columns = new List <ListColumn>(); string columnFile = string.Format("{0}\\Layout\\Columns\\{1}Columns.xml", Application.StartupPath, moduleName); // 模块的列配置文件(xml文件) if (!File.Exists(columnFile)) { return(columns); } try { //var streamReader = new StreamReader(columnFile); //string xmlString = streamReader.ReadToEnd(); //streamReader.Close(); var xmlDoc = new XmlDocument(); //xmlDoc.Load(new StringReader(xmlString)); //载入XML xmlDoc.Load(columnFile); XmlNode root = xmlDoc.SelectSingleNode("/columns"); //获取根结点 if (root == null) { return(null); } var listColumn = new ListColumn(); foreach (XmlNode columnNode in root.ChildNodes) { listColumn.Caption = columnNode.SelectSingleNode("caption").InnerText.Trim(); listColumn.FieldName = columnNode.SelectSingleNode("fieldname").InnerText.Trim(); listColumn.Width = Convert.ToInt32(columnNode.SelectSingleNode("width").InnerText.Trim()); listColumn.IsAdvanced = columnNode.SelectSingleNode("isadvanced").InnerText.Trim() == "Y"; columns.Add(listColumn); } } catch (Exception ex) { ExManager.ReportException(ex); return(columns); } return(columns); }
/// ############################################################### /// <summary> /// 处理Between方法两个日期的时间部分(将开始时间转换为00:00:00,结束时间转换为23:59:59) /// Coder: Sum(20120806) /// </summary> /// <param name="dtBeginDate">开始时间</param> /// <param name="dtEndDate">结束时间</param> /// <returns>是否成功转换</returns> public static bool GetBetweenDate(ref DateTime dtBeginDate, ref DateTime dtEndDate) { try { if (dtBeginDate != DateTime.MinValue && dtEndDate != DateTime.MinValue) { dtBeginDate = dtBeginDate.Date; dtEndDate = dtBeginDate.Date.AddDays(+1).AddSeconds(-1); return(true); } return(false); } catch (Exception ex) { ExManager.ReportException(ex); return(false); } }
///################################################################################# /// <summary> /// 默认的输入框 /// Coder:ryo 20060519 /// </summary> /// <returns></returns> ///################################################################################# public static string GetText() { try { FrmInputBox frmib = new FrmInputBox(); frmib.ShowDialog(); if (frmib.tbText.EditValue == null) { return(null); } else { return(frmib.tbText.EditValue.ToString()); } } catch (Exception ex) { ExManager.ReportException(ex, true); return(""); } }
///######################################################################### /// <summary> /// 复制文件到指定目录。 如果指定目录不存在,则创建该目录 /// Coder: Kevin Wang(2010-09-23) /// </summary> /// <param name="sSource">源文件路径,全路径+文件名</param> /// <param name="sTarget">目标文件路径,全路径+文件名</param> ///<param name="bOverride">如果目标目录下有同名文件,是否覆盖</param> ///<returns>成功返回true,否则返回false</returns> public static bool CopyFile(string sSource, string sTarget, bool bOverride) { try { if (!Directory.Exists(Path.GetDirectoryName(sTarget))) { // 如果目标路径不存在,则创建目标路径 Directory.CreateDirectory(Path.GetDirectoryName(sTarget)); } //如果目标文件已存在,先将其设为非只读 SetFileReadOnly(sTarget, false); File.Copy(sSource, sTarget, bOverride); //保存后再次设为非只读 SetFileReadOnly(sTarget, false); return(true); } catch (Exception ex) { ExManager.ReportException(ex); return(false); } return(true); }
///######################################################################## /// <summary> /// 设置文件的只读属性 /// Coder:lixin(2010-11-17) /// </summary> /// <param name="sPath">文件的绝对路径</param> /// <param name="bReadOnly">true:设为只读 false:设为非只读</param> /// <returns>true:设置成功 false:失败</returns> public static bool SetFileReadOnly(string sPath, bool bReadOnly) { if (File.Exists(sPath) == false) { return(false); } try { FileInfo fileInfo = new FileInfo(sPath); if (fileInfo.Attributes.ToString().IndexOf("ReadOnly") != -1) { fileInfo.Attributes = bReadOnly ? FileAttributes.ReadOnly : FileAttributes.Normal; } } catch (Exception ex) { ExManager.ReportException(ex); return(false); } return(true); }
///################################################################################# /// <summary> /// 带提示文字以及最大输入长度的输入框 /// Coder:ryo 20060519 /// </summary> /// <returns></returns> ///################################################################################# public static string GetText(string Note, int MaxLength) { try { FrmInputBox frmib = new FrmInputBox(); frmib.lbText.Text = Note; frmib.tbText.Properties.MaxLength = MaxLength; frmib.ShowDialog(); if (frmib.tbText.EditValue == null) { return(null); } else { return(frmib.tbText.EditValue.ToString()); } } catch (Exception ex) { ExManager.ReportException(ex, true); return(""); } }