示例#1
0
        /// <summary>
        /// 将内存流输出到文件
        /// </summary>
        /// <param name="FileName">文件名</param>
        /// <param name="In">内存流</param>
        /// <exception cref="CFileException">
        /// 写入失败触发返回 ExceptionCode.FileReadFail异常
        /// </exception>
        public static void WriteStream(String FileName, MemoryStream In)
        {
            bool       FileOpened = false;
            FileStream fs         = null;

            try
            {
                if (File.Exists(FileName))
                {
                    File.Delete(FileName);
                }

                fs = new FileStream(FileName, FileMode.CreateNew);
                In.WriteTo(fs);
                fs.Close();
            }
            catch (Exception e)
            {
                if (FileOpened)
                {
                    try
                    {
                        fs.Close();
                    }
                    catch
                    {
                    }
                }

                CFileException fse = new CFileException(ExceptionCode.FileWriteFail
                                                        , String.Format("Write File : {0} Fail!", FileName), e);
                fse.Raise();
            }
        }
示例#2
0
        /// <summary>
        /// 文件读入到字符数组中
        /// </summary>
        /// <param name="FileName">文件名</param>
        /// <param name="buf">输出字符数组</param>
        /// <param name="Encode">
        /// 编码方式
        /// 如 "gb2312" "utf-8"
        /// </param>
        /// <exception cref="CFileException">
        /// 读入失败触发返回 ExceptionCode.FileReadFail异常
        /// </exception>
        static public void ReadFileToChars(String FileName, out Char[] buf, String Encode)
        {
            bool       FileOpened = false;
            FileStream fs         = null;

            buf = null;

            try
            {
                fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
                CStream.ReadStreamToChars(fs, out buf, Encode);
                fs.Close();
            }
            catch (Exception e)
            {
                if (FileOpened)
                {
                    try
                    {
                        fs.Close();
                    }
                    catch
                    {
                    }
                }

                CFileException fse = new CFileException(ExceptionCode.FileReadFail
                                                        , String.Format("Read File : {0} Fail!", FileName), e);
                fse.Raise();
            }
        }
示例#3
0
        /// <summary>
        /// 将文件读入到一个String对象中
        /// </summary>
        /// <param name="FileName">文件名</param>
        /// <param name="Encode">编码方式</param>
        /// <returns>String 对象</returns>
        /// <exception cref="CFileException">
        /// 读入失败触发返回 ExceptionCode.FileReadFail异常
        /// </exception>
        static public String ReadFileToString(String FileName, String Encode)
        {
            bool       FileOpened = false;
            FileStream fs         = null;
            String     str;

            try
            {
                fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
                CStream.ReadStreamToString(fs, out str, Encode);
                fs.Close();
                return(str);
            }
            catch (Exception e)
            {
                if (FileOpened)
                {
                    try
                    {
                        fs.Close();
                    }
                    catch
                    {
                    }
                }

                CFileException fse = new CFileException(ExceptionCode.FileReadFail
                                                        , String.Format("Read File : {0} Fail!", FileName), e);
                fse.Raise();
                return(null);
            }
        }
示例#4
0
        /// <summary>
        /// 把文件读取到内存流中
        /// </summary>
        /// <param name="FileName">文件名</param>
        /// <returns>内存流</returns>
        /// <exception cref="CFileException">
        /// 读入失败触发返回 ExceptionCode.FileReadFail异常
        /// </exception>
        static public MemoryStream ReadFileToStream(String FileName)
        {
            byte[]     Bytes      = new byte[32768];
            int        read       = 0;
            int        offset     = 0;
            FileStream fs         = null;
            bool       FileOpened = false;

            try
            {
                MemoryStream mem = new MemoryStream();
                fs           = new FileStream(FileName, FileMode.Open, FileAccess.Read);
                FileOpened   = true;
                mem.Position = 0;

                while ((read = fs.Read(Bytes, 0, Bytes.Length)) > 0)
                {
                    offset += read;
                    mem.Write(Bytes, 0, read);
                }

                fs.Close();
                return(mem);
            }
            catch (Exception e)
            {
                if (FileOpened)
                {
                    try
                    {
                        fs.Close();
                    }
                    catch
                    {
                    }
                }

                CFileException fse = new CFileException(ExceptionCode.FileReadFail
                                                        , String.Format("Read File : {0} Fail! ErrMsg={1}", FileName, e.Message), e);
                fse.Raise();
                return(null);
            }
        }
示例#5
0
        /// <summary>
        /// 向文件末尾追加一行
        /// </summary>
        /// <param name="str">要追加的字符串</param>
        /// <param name="FileName">文件名</param>
        /// <param name="Encode">编码方式</param>
        /// <exception cref="CFileException">
        /// 写入失败触发返回 ExceptionCode.FileReadFail异常
        /// </exception>
        public static void WriteLine(String FileName, String str, String Encode)
        {
            TextWriter writer     = null;
            bool       FileOpened = false;
            FileStream fs         = null;

            try
            {
                if (File.Exists(FileName))
                {
                    fs = new FileStream(FileName, FileMode.Append);
                }
                else
                {
                    fs = new FileStream(FileName, FileMode.CreateNew);
                }


                fs.Seek(0, SeekOrigin.End);
                writer = new StreamWriter(fs, Encoding.GetEncoding(Encode));
                writer.WriteLine(str);
                writer.Close();
                fs.Close();
            }
            catch (Exception e)
            {
                if (FileOpened)
                {
                    try
                    {
                        fs.Close();
                    }
                    catch
                    {
                    }
                }

                CFileException fse = new CFileException(ExceptionCode.FileWriteFail
                                                        , String.Format("Write File : {0} Fail!", FileName), e);
                fse.Raise();
            }
        }