C#向指定路径写入文件

来源:互联网 发布:芜湖网络推广 编辑:程序博客网 时间:2024/06/05 17:08
        /// <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();
                }
            }
        }
0 0