Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > .NET > C#.NET > .NET平台下读取文件编码为BASE64 (C#语言实现)
【标  题】:.NET平台下读取文件编码为BASE64 (C#语言实现)
【关键字】:.NET,BASE64,C#
【来  源】:http://blog.csdn.net/lixiaobai/archive/2006/08/08/1038280.aspx

.NET平台下读取文件编码为BASE64 (C#语言实现)

Your Ad Here     编程中有时候需要把2进制文件作为文本来处理, 下面的代码实现直接读取一个2进制文件,保存为BASE64格式的文本

        /// <summary>
        /// 读取文件编码为BASE64
        /// </summary>
        /// <param name="FilePathX">需要转换的文件路径</param>
        public String EncodeWithString(String FilePathX)
        {
            System.IO.FileStream inFile;
            byte[] binaryData;

            try
            {
                inFile = new System.IO.FileStream(FilePathX,
                                                  System.IO.FileMode.Open,
                                                  System.IO.FileAccess.Read);

                binaryData = new Byte[inFile.Length];

                long bytesRead = inFile.Read(binaryData, 0,
                                            (int)inFile.Length);
                inFile.Close();
            }
            catch (System.Exception exp)
            {
                // Error creating stream or reading from it.
                System.Console.WriteLine("{0}", exp.Message);
                return "Error";
            }

            // Convert the binary input into Base64 UUEncoded output.
            string base64String;
            try
            {
                base64String =
                   System.Convert.ToBase64String(binaryData,
                                                 0,
                                                 binaryData.Length);
                return base64String;
            }
            catch (System.ArgumentNullException)
            {
                System.Console.WriteLine("Binary data array is null.");
                return "Error";
            }

        }
我的开源尝试:SharpFormEditor 1.0:【上一篇】
WinCVS中文版:【下一篇】
【相关文章】
  • .Net Framework3.0 实践
  • c#语言规范1.2版本(一) ------想学c#的不用买书了
  • 一个三层架构的WinForms程序的完整范例(.NET 1.1/Northwind)
  • 利用消息机制实现.NET AOP(面向方面编程) --三种实现代码下载及说明
  • asp.net控件开发(一)简单属性、视图状态、控件状态
  • 用IronPython作为.Net的脚本语言。
  • ASP.NET 2.0 BuildProvider 导论(一)
  • C#中,参数传递ref,out之间的区别
  • C#操作数据仓库
  • 使用 Anthem.NET 框架的一个调试经历
  • 【随机文章】
  • AWK 程式中使用 Shell 命令
  • Linux的用户管理
  • 深入理解java数组
  • iptables's man
  • 从今日起不再用QQ空间了
  • ORA-01001 and ORACLE游标
  • 令人深思的批评
  • tomcat下中文问题的解决办法
  • 页面打印
  • I/O重定向
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.