Beispiel #1
0
        /// <summary>
        /// 将字符串追加到指定的文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="context"></param>
        /// <param name="encoding"></param>
        public static void StringToFile(string fileName, string context, Encoding encoding)
        {
            if (null == encoding)
            {
                encoding = Encoding.UTF8;
            }
            if (!FileUnitity.FileExist(fileName))
            {
                FileUnitity.CreateEmptyFile(fileName);
            }

            StreamWriter sw = null;

            try
            {
                lock (_lock)
                {
                    sw = File.AppendText(fileName);
                    sw.WriteLine(context);
                }
            }
            catch
            {}
            finally
            {
                if (sw != null)
                {
                    sw.Flush();
                    sw.Close();
                    sw.Dispose();
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// 创建空文件
        /// </summary>
        /// <param name="fileName"></param>
        public static void CreateEmptyFile(string fileName)
        {
            if (FileUnitity.FileExist(fileName))
            {
                throw new Exception("文件已经存在,不可在创建....");
            }
            FileStream fsteam = File.Create(fileName);

            fsteam.Close();
            fsteam.Dispose();
        }
Beispiel #3
0
        /// <summary>
        /// 将字符串 存成新的文件,如果文件存在就删除
        /// </summary>
        /// <param name="fileName">文件名(含路径)</param>
        /// <param name="context">保存的内容</param>
        /// <param name="encoding">编码方式</param>
        public static void StringToNewFile(string fileName, string context, Encoding encoding)
        {
            if (null == encoding)
            {
                encoding = Encoding.UTF8;
            }
            if (FileUnitity.FileExist(fileName))
            {
                FileUnitity.DeleteFile(fileName);
            }
            StreamWriter fstreamwr = new StreamWriter(fileName, true, encoding);

            fstreamwr.WriteLine(context);
            fstreamwr.Close();
            fstreamwr.Dispose();
        }