程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> IO操作(C#)

IO操作(C#)

編輯:C#入門知識

IO操作(C#)


在一些游戲當中 , 通常將一些參數寫在配置裡面 , 無論是XML  , EXCEL , TXT , BIN …… 我相信都會用到其中的一種 。首先 , XML , BIN 對於策劃人員來說就很容易配置錯誤了。如果能用程序將Excel裡面的數據轉化為Txt , Bin , Xml數據, 就很爽了 , 關於Excel數據的讀寫我在前幾篇博文中寫過。 這裡就涉及到了I/O的操作 。廢話不多說 , 上代碼 . 這裡寫的都是同步I/O操作 。   309
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; 
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="targetPath"></param>
        /// <returns></returns>
        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;
                }
            }
        }
        /// <summary>
        /// 獲得電腦所有的驅動盤
        /// </summary>
        /// <returns></returns>
        public string[] GetMyLogicalDrives()
        {
            return Directory.GetLogicalDrives();
        }
        /// <summary>
        /// 移動數據
        /// </summary>
        /// <param name="oldPath"> 原始的路徑  </param>
        /// <param name="newPath"> 新的路徑 </param>
        /// <returns> 操作是否成功 </returns>
        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;
            }
        }
        /// <summary>
        /// 復制一個文件
        /// </summary>
        /// <param name="oldPath"></param>
        /// <param name="newPath"></param>
        /// <returns></returns>
        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;
            }
        }
        /// <summary>
        /// 刪除一個文件
        /// </summary>
        /// <param name="targetPath"></param>
        /// <returns></returns>
        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;
            }
        }
        /// <summary>
        /// 創建一個文件夾
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        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;
                }
            }
        }
        /// <summary>
        /// 刪除文件夾
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        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;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="oldPath"></param>
        /// <param name="newPath"></param>
        /// <returns></returns>
        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;
            }
        }
        /// <summary>
        /// 讀取文件( 一個個讀 )老是在流以外 , 無法讀到正確的值
        /// </summary>
        /// <param name="targetPath"></param>
        /// <returns></returns>
        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;
            }
        }
        /// <summary>
        /// 讀取文本
        /// </summary>
        /// <param name="targetPath"></param>
        /// <returns></returns>
        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;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="targetPath"></param>
        /// <param name="content"></param>
        /// <param name="isNendWarp"></param>
        /// <returns></returns>
        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;
            }
        }
    }
}

 


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved