statictools.cs

来自「飞信的收发使用csharp进行开发」· CS 代码 · 共 66 行

CS
66
字号
namespace Imps.Client.Pc
{
    using Imps.Client.Resource;
    using System;
    using System.IO;
    using System.Text;

    internal static class StaticTools
    {
        private static object lockObject = new object();

        public static string GetFullPath(string path)
        {
            if (path.IndexOf(':') == 1)
            {
                return path;
            }
            return Path.Combine(ImpsPathInfo.ApplicationDataDir, path);
        }

        public static bool IsEmptyDirectory(string path)
        {
            return (!Directory.Exists(path) || ((Directory.GetFiles(path).Length == 0) && (Directory.GetDirectories(path).Length == 0)));
        }

        public static void LogError(string logFile, Exception ex)
        {
            if (ex is LiveUpdateException)
            {
                LiveUpdateException exception = ex as LiveUpdateException;
                string message = exception.Message;
                if (exception.InnerException != null)
                {
                    message = message + "   信息:" + exception.InnerException.Message;
                }
                LogMessage(logFile, message);
            }
            else
            {
                LogMessage(logFile, ex.Message);
            }
        }

        public static void LogMessage(string logFile, string message)
        {
            if (string.IsNullOrEmpty(logFile))
            {
                throw new ArgumentNullException("logFile");
            }
            if (!string.IsNullOrEmpty(message))
            {
                logFile = GetFullPath(logFile);
                if (!Directory.Exists(Path.GetDirectoryName(logFile)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(logFile));
                }
                lock (lockObject)
                {
                    File.AppendAllText(logFile, "[" + DateTime.Now.ToString() + "] \r" + message + "\r\n", Encoding.UTF8);
                }
            }
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?