资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

IO操作(C#)-创新互联

在一些游戏当中 , 通常将一些参数写在配置里面 , 无论是XML  , EXCEL , TXT , BIN …… 我相信都会用到其中的一种 。首先 , XML , BIN 对于策划人员来说就很容易配置错误了。如果能用程序将Excel里面的数据转化为Txt , Bin , Xml数据, 就很爽了 , 关于Excel数据的读写我在前几篇博文中写过。 这里就涉及到了I/O的操作 。废话不多说 , 上代码 . 这里写的都是同步I/O操作 。

为贵南等地区用户提供了全套网页设计制作服务,及贵南网站建设行业解决方案。主营业务为网站设计、成都网站设计、贵南网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ComprehensiveTest.com.myio
{
    public class IoManager
    {
        private static IoManager instance = null;

        public static IoManager Instance
        {
            get {
                if (IoManager.instance == null)
                {
                    IoManager.instance = new IoManager();
                }
                return IoManager.instance; 
            }
        }
        /// 
        /// 
        /// 
        /// 
        /// 
        public bool CreateFile(string targetPath)
        {
            if (File.Exists(targetPath))
            {
                return true;
            }
            else
            {
                try
                {
                    //使用这2种方法都可以
                    //FileStream file = File.Create(targetPath);
                    FileStream file = new FileStream(targetPath, FileMode.Create);
                    file.Close();
                    return true;
                }
                catch (Exception e)
                {
                    Console.WriteLine("创建文件{0},失败 , 原因 : {1} ", targetPath, e.ToString());
                    return false;
                }
            }
        }
        /// 
        /// 获得电脑所有的驱动盘
        /// 
        /// 
        public string[] GetMyLogicalDrives()
        {
            return Directory.GetLogicalDrives();
        }
        /// 
        /// 移动数据
        /// 
        ///  原始的路径  
        ///  新的路径 
        ///  操作是否成功 
        public bool MoveFile(string oldPath, string newPath)
        {
            if (File.Exists(oldPath))
            {
                try
                {
                    File.Move(oldPath, newPath);
                    return true;
                }
                catch (Exception e)
                { 
                    Console.WriteLine("移动文件{0},失败 , 原因 : {1} " , oldPath , e.ToString() );
                    return false;
                }
            }
            else
            {
                Console.WriteLine("Error , {0}文件不存在!!! " , oldPath );
                return false;
            }
        }
        /// 
        /// 复制一个文件
        /// 
        /// 
        /// 
        /// 
        public bool CopyFile(string oldPath, string newPath)
        {
            if (File.Exists(oldPath))
            {
                try
                {
                    File.Copy(oldPath, newPath);
                    return true;
                }
                catch (Exception e)
                {
                    Console.WriteLine("复制文件{0},失败 , 原因 : {1} ", oldPath, e.ToString());
                    return false;
                }
            }
            else
            {
                Console.WriteLine("Error , {0}文件不存在!!! ", oldPath);
                return false;
            }
        }
        /// 
        /// 删除一个文件
        /// 
        /// 
        /// 
        public bool DeleteFile( string targetPath )
        {
            if(File.Exists( targetPath ))
            {
                try
                {
                    File.Delete(targetPath);
                    return true;
                }
                catch (Exception e)
                {
                    Console.WriteLine("删除文件{0},失败 , 原因 : {1} ", targetPath, e.ToString());
                    return false;
                }
            }
            else
            {
                Console.WriteLine("Error , {0}文件不存在!!! ", targetPath);
                return false;
            }
        }
        /// 
        /// 创建一个文件夹
        /// 
        /// 
        /// 
        public bool CreateFolder(string path)
        {
            if (Directory.Exists(path))
            {
                Console.WriteLine("文件夹{0}已经存在", path);
                return true;
            }
            else
            {
                try
                {
                    DirectoryInfo dirInfo = Directory.CreateDirectory(path);
                    Console.WriteLine("创建文件夹成功 , 创建时间为{0}", Directory.GetCreationTime(path));
                    return true;
                }
                catch (Exception e)
                {
                    Console.WriteLine("创建文件夹失败 , 失败原因{0}", e.ToString());
                    return false;
                }
            }
        }
        /// 
        /// 删除文件夹
        /// 
        /// 
        /// 
        public bool DeleteFolder(string path)
        {
            if (Directory.Exists(path))
            {
                try
                {
                    Directory.Delete(path);
                    return true;
                }
                catch (Exception e)
                {
                    Console.WriteLine("删除文件夹失败 , 失败原因{0}", e.ToString());
                    return false;
                }
            }
            else
            {
                return true;
            }
        }
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public bool MoveFolder(string oldPath , string newPath)
        {
            if (Directory.Exists(oldPath))
            {
                try
                {
                    Directory.Move(oldPath, newPath);
                    return true;
                }
                catch (Exception e)
                {
                    Console.WriteLine("移动文件夹{0},失败 , 原因 : {1} ", oldPath, e.ToString());
                    return false;
                }
            }
            else
            {
                Console.WriteLine("Error , {0}文件夹不存在!!! ", oldPath);
                return false;
            }
        }
        /// 
        /// 读取文件( 一个个读 )老是在流以外 , 无法读到正确的值
        /// 
        /// 
        /// 
        public bool ReadOneByOneTest(string targetPath)
        {
            if (File.Exists(targetPath))
            {
                FileStream fs = new FileStream(targetPath, FileMode.Open, FileAccess.Read);
                BinaryReader br = new BinaryReader(fs);
                br.BaseStream.Seek(0, SeekOrigin.Begin); //将指针设到开头
                while (br.BaseStream.Position < br.BaseStream.Length)
                {
                    try
                    {
                        Console.WriteLine(br.ReadString());
                    }
                    catch (EndOfStreamException e)
                    {
                        Console.WriteLine("已经到了结尾 {0}", e.ToString());
                    }
                }
                br.Close();
                fs.Close();
                return true;
            }
            else
            {
                return false;
            }
        }
        /// 
        /// 读取文本
        /// 
        /// 
        /// 
        public bool ReadCommon(string targetPath)
        {
            if (File.Exists(targetPath))
            {
                //using (StreamReader sr = File.OpenText(targetPath)) // 读中文将乱码
                using( StreamReader sr = new StreamReader( targetPath , UnicodeEncoding.GetEncoding("GB2312"))) // 解决中文乱码问题
                {
                    string readStr;
                    while ((readStr = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(readStr);
                    }
                    sr.Close();
                }
                return true;
            }
            else
            {
                return false;
            }
        }
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public bool WriteCommon(string targetPath , string content , bool isNendWarp )
        {
            if (File.Exists(targetPath))
            {
                //using (StreamWriter sw = File.AppendText(targetPath)) // 中文乱码
                using( StreamWriter sw = new StreamWriter( targetPath , true ,UnicodeEncoding.GetEncoding("GB2312"))) // 解决中文乱码问题
                {
                    if (isNendWarp)
                    {
                        sw.WriteLine(content);
                    }
                    else
                    {
                        sw.Write(content);
                    }
                    sw.Close();
                }
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

StreamWriter( targetPath , true ,UnicodeEncoding.GetEncoding("GB2312"))

第二个参数为false : 表示覆盖写入

这里我都测过了 , OK的 , 有一个方法 : ReadOneByOneTest 老是读不到数据 :

IO操作(C#)

望高手指点!

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


文章名称:IO操作(C#)-创新互联
网站URL:http://cdkjz.cn/article/coigch.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220