C#向指定路徑寫入文件

        /// <summary>
        /// 向指定路徑寫入文件
        /// </summary>
        /// <param name="LogPath"></param>
        public void WriteTestFlagFile(string LogPath)
        {
            try
            {
                if (!(File.Exists(LogPath)))
                {//如果這個文件不存在,就創建這個文件
                    FileStream aFile = new FileStream(LogPath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                    StreamWriter sw = new StreamWriter(aFile);
                    sw.WriteLine("TestCompleteFlag");
                    sw.Close();                                     //釋放文件
                    aFile.Close();
                    sw.Dispose();                                   //釋放資源
                    aFile.Dispose();
                }
            }
            catch (Exception)
            {//寫入文件異常,這個地方一般是由於使用了非超級管理員權限向C盤寫入了文件,報告權限不夠
                if (MessageBox.Show("Run me in the role of the super administrator!", "Information Tip:", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.OK)
                {
                    this.Dispose();
                    Application.Exit();
                }
            }
        }
發佈了116 篇原創文章 · 獲贊 79 · 訪問量 82萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章