文件写操作--WriteLog

private static void Write(string sMsg, string fileName)

        {

            if (sMsg != "")

            {

                try

                {

                    var dir = new FileInfo(Assembly.GetEntryAssembly().Location).DirectoryName + "\\log\\";

                    if (!Directory.Exists(dir))

                    {

                        Directory.CreateDirectory(dir);

                    }

                    var fi =new FileInfo( dir + fileName+".log");

                    if (!fi.Exists)

                    {

                        using (var sw = fi.CreateText())

                        {

                            sw.WriteLine(DateTime.Now + ": " + sMsg + "\n");

                            sw.Close();

                        }

                    }

                    else

                    {

                        using (var sw = fi.AppendText())

                        {

                            sw.WriteLine(DateTime.Now + ": " + sMsg + "\n");

                            sw.Close();

                        }

                    }

                }

                catch (Exception ex)

                {

                    Console.WriteLine(ex.Message);

                }

            }

        }

  

 

public static bool Reset()

        {

            try

            {

                lock (_readWriteLock)

                {

                    var fi = new FileInfo("Data/ValidTickets_bak.xml");

                    fi.CopyTo(_xmlFileName, true);

                }

            }

            catch (Exception ex)

            {

                Logger.WriteLogger(ex, ConstValues.MainLogFileName);

                return false;

            }

            return true;

        }

  

你可能感兴趣的:(write)