C#中简单的写日志的方法

public static void WriteLog(string msg)
        {
            string appPath = AppDomain.CurrentDomain.BaseDirectory;
            string ServerPath = appPath + @"\Log" + DateTime.Now.ToString("yyyyMMdd") + ".txt";

StreamWriter srWriter = new StreamWriter(ServerPath, true);
            string WriteStr = "************************************************************************************************\r\n";
            WriteStr += "Date:" + DateTime.Now.ToString() + "  \r\n" + msg;
            srWriter.WriteLine(WriteStr);
            srWriter.Close();
        }

另附上js的提示的换行写法:

alert("您确定提交整个问卷吗?\n\n提交后将不能再填写各个工种的用工需求,整个问卷将就此结束!")

confirm("您确定提交整个问卷吗?\n\n提交后将不能再填写各个工种的用工需求,整个问卷将就此结束!")

//事物处理,catch部分是写日志的方法
        public static bool TransactionAction(string SQL, string TransactionName)
        {
            SqlConnection myConnection = new SqlConnection(SQLServer.SQLConnectionString);
            myConnection.Open();
            SqlCommand myCommand = myConnection.CreateCommand();
            SqlTransaction myTrans;
            myTrans = myConnection.BeginTransaction(IsolationLevel.ReadCommitted, TransactionName);
            myCommand.Connection = myConnection;
            myCommand.Transaction = myTrans;
            try
            {
                myCommand.CommandText = SQL;
                myCommand.ExecuteNonQuery();

myTrans.Commit();
                return true;
            }
            catch (Exception ee)
            {
                string path = @"D:\webroot\ErrorLog";
                DirectoryInfo di = Directory.CreateDirectory(path); //在path下创建文件夹
                string strPath = path + "file://vipspace_error/_" + System.DateTime.Now.ToString("yyyyMMdd") + ".txt";
                StreamWriter srWriter = new StreamWriter(new FileStream(strPath, FileMode.OpenOrCreate), Encoding.GetEncoding("Unicode"));
                string WriteStr = "Date:" + DateTime.Now.ToString() + "  Error:" + ee.Message.ToString() + " SQL:" + SQL;
                srWriter.WriteLine(WriteStr);
                srWriter.Flush();
                srWriter.Close();

myTrans.Rollback(TransactionName);
                return false;
            }
            finally
            {
                myConnection.Close();
            }
        }

判断文件是否存在,如存在并将其删除

public static bool IsExistsFile(string FilePath)
{
if (File.Exists(FilePath) == true)

//****删除文件
File.Delete(FilePath);
return true;
}
return false;

上一篇:HttpClient使用详解


下一篇:基于内容的图片检索CBIR(Content Based Image Retrieval)简介