⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cfile.cs

📁 KTDictSeg 简介: KTDictSeg 是由KaiToo搜索开发的一款基于字典的简单中英文分词算法 * 主要功能: 中英文分词
💻 CS
📖 第 1 页 / 共 2 页
字号:
        /// </exception>
        static public StringBuilder ReadFileToStringBuilder(String FileName, String Encode)
        {
            String temp;
            temp = ReadFileToString(FileName, Encode);

            StringBuilder ret = new StringBuilder(temp);
            return ret;
        }

        /// <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();
            }

        }

        /// <summary>
        /// 将字符串拷贝到文件中
        /// </summary>
        /// <param name="str"></param>
        /// <param name="FileName"></param>
        /// <param name="Encode"></param>
        /// <remarks>
        /// 调用该函数源文件将会被清空
        /// </remarks>
        /// <exception cref="CFileException">
        /// 写入失败触发返回 ExceptionCode.FileReadFail异常
        /// </exception>
        public static void WriteString(String FileName, String str, String Encode)
        {
            TextWriter writer = null;
            bool FileOpened = false;
            FileStream fs = null;

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

                fs = new FileStream(FileName, FileMode.CreateNew);
                writer = new StreamWriter(fs, Encoding.GetEncoding(Encode));
                writer.Write(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();
            }
        }

        /// <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();
            }
        }

        /// <summary>
        /// 获取绝对路径
        /// </summary>
        /// <param name="BasePath">基准路径</param>
        /// <param name="RelPath">相对路径</param>
        /// <returns>绝对路径,绝对路径以"\"结尾</returns>
        /// <exception>
        /// ArgumentException
        /// SecurityException
        /// ArgumentNullException
        /// NotSupportedException
        /// PathTooLongException
        /// 具体见Path.GetFullPath的帮助
        /// </exception>
        public static String GetAbsPath(String BasePath, String RelPath)
        {
            Environment.CurrentDirectory = BasePath;
            String ret = Path.GetFullPath(RelPath);
            if (ret[ret.Length - 1] != '\\' && ret[ret.Length - 1] != '/')
            {

                ret += "\\";
            }

            return ret ;
        }

        /// <summary>
        /// 获取纯文件名
        /// </summary>
        /// <param name="PathName"></param>
        /// <returns>纯文件名</returns>
        /// <exception>
        /// ArgumentException
        /// SecurityException
        /// ArgumentNullException
        /// NotSupportedException
        /// PathTooLongException
        /// 具体见Path.GetFileName的帮助
        /// </exception>        
        public static String GetFileName(String PathName)
        {
            return Path.GetFileName(PathName);
        }

        /// <summary>
        /// 获取不带扩展名的纯文件名
        /// </summary>
        /// <param name="PathName"></param>
        /// <returns>不带扩展名的纯文件名</returns>
        /// <exception>
        /// ArgumentException
        /// SecurityException
        /// ArgumentNullException
        /// NotSupportedException
        /// PathTooLongException
        /// 具体见Path.GetFileNameWithoutExtension的帮助
        /// </exception>        
        public static String GetFileNameWithoutExt(String PathName)
        {
            return Path.GetFileNameWithoutExtension(PathName);
        }

        /// <summary>
        /// 获取文件扩展名
        /// </summary>
        /// <param name="PathName"></param>
        /// <returns>
        /// 文件扩展名,扩展名包括前面的.
        /// 如输入 a.txt ,返回 .txt
        /// </returns>
        /// <exception>
        /// ArgumentException
        /// SecurityException
        /// ArgumentNullException
        /// NotSupportedException
        /// PathTooLongException
        /// 具体见Path.GetExtension的帮助
        /// </exception>        
        public static String GetExt(String PathName)
        {
            return Path.GetExtension(PathName);
        }

        /// <summary>
        /// 获取文件的路径名
        /// </summary>
        /// <param name="PathName"></param>
        /// <returns>返回文件的路径,不包括文件名。路径以"\"结尾</returns>
        /// <exception>
        /// ArgumentException
        /// SecurityException
        /// ArgumentNullException
        /// NotSupportedException
        /// PathTooLongException
        /// 具体见Path.GetDirectoryName的帮助
        /// </exception>        
        public static String GetPath(String PathName)
        {
            String ret = Path.GetDirectoryName(PathName);
            if (ret[ret.Length - 1] != '\\')
            {

                ret += "\\";
            }

            return ret;
        }

        /// <summary>
        /// 把Base64编码的字符串转换为实际数据存入文件
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <param name="data">Base64编码的字符串</param>
        /// <remarks>文件操作可能触发异常</remarks>
        static public void WriteFileByBase64String(String fileName, String data)
        {
            byte[] buf = Convert.FromBase64String(data);
            FileStream fs = new FileStream(fileName, FileMode.Create);
            fs.Write(buf, 0, buf.Length);
            fs.Close();
        }


        /// <summary>
        /// 把文件内容读入Base64编码的字符串中
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <returns>Base64编码的字符串</returns>
        /// <remarks>文件操作可能触发异常</remarks>
        static public String GetBase64StringFromFile(String fileName)
        {
            FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            if (stream == null)
            {
                return null;
            }

            byte[] buf = new byte[stream.Length];
            stream.Position = 0;
            stream.Read(buf, 0, (int)stream.Length);
            stream.Close();
            return Convert.ToBase64String(buf);

        }

        /// <summary>
        /// 获取文件版本号
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <returns>如果文件有版本号,返回版本号,否则返回null</returns>
        static public String GetFileVersionNo(String fileName)
        {
            try
            {
                FileVersionInfo fileInfo = FileVersionInfo.GetVersionInfo(fileName);
                return fileInfo.FileVersion;
            }
            catch
            {
                return null;
            }
        }


    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -