讀取配置文件/創建配置文件

const string LOG4NET_CONFIG_FILE_NAME = "NLog.config";

        private void button1_Click(object sender, EventArgs e)
        {
            string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, LOG4NET_CONFIG_FILE_NAME);
            if (!File.Exists(fileName))
                WriteLog4NetConfig(fileName);

            infoLogger = LoggerHelper.LoggerInfoInstance;
            //Logger.Info("Hello");
            //Logger.Error("Hello");
            //Logger1.Error("Hello");
            
            try
            {
                //Thread.Sleep(5000);
                infoLogger.Log(Logger, "Test21212121");
            }
            catch (Exception ex)
            {

                throw;
            }

            var T = Activator.CreateInstance(typeof(Form1));

            int agvNo = 2;

            //if (agvNo > 0 && (MessageBox.Show("當前車輛正在執行任務,刪除之後可能會導致車輛異常,確認刪除?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes))
            //{

            //}

            StringBuilder sb = new StringBuilder(100);
            sb.Append("delete from Task_Info_SaiMo where StartTime ='");

            sb.Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "'|| StartTime ='");
        }

        private void WriteLog4NetConfig(string fileName)
        {
            FileStream fs = null;
            Stream xmlStream = null;
            byte[] bytsXml;
            int lenByts;

            try
            {
                Assembly asmCurrent = Assembly.GetExecutingAssembly();
                xmlStream = asmCurrent.GetManifestResourceStream(typeof(Form2).Namespace
                    + "." + LOG4NET_CONFIG_FILE_NAME);
                bytsXml = new byte[xmlStream.Length];
                using (fs = File.Create(fileName))
                {
                    while ((lenByts = xmlStream.Read(bytsXml, 0, bytsXml.Length)) > 0)
                    {
                        //向文件中寫信息
                        fs.Write(bytsXml, 0, lenByts);
                        fs.Flush();
                    }
                }
            }
            finally
            {
                if (fs != null)
                    fs.Close();

                if (xmlStream != null)
                    xmlStream.Close();
            }
        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章