IO operation (C #)
In some games, some parameters are usually written in the configuration, such as XML, EXCEL, TXT, BIN ...... I believe one of them will be used. First, XML and BIN are easy to configure incorrectly for planners. If you can use a program to convert the data in Excel into Txt, Bin, and Xml data, it will be nice. I have written about Excel Data in the previous blog posts. I/O operations are involved here. Not to mention that I/O operations are synchronized here. 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> //// </sum Mary> /// <param name = "targetPath"> </param> /// <returns> </returns> public bool CreateFile (string targetPath) {if (File. exists (targetPath) {return true;} else {try {// both methods can be used // FileStream file = File. create (targetPath); FileStream file = new FileStream (targetPath, FileMode. create); file. close (); return true;} catch (Exception e) {Console. writeLine ("creating file {0}, failed, cause: {1}", targetPath, e. ToString (); return false ;}}} /// <summary> /// obtain all drive disks of the Computer /// </summary> /// <returns> </returns> public string [] GetMyLogicalDrives () {return Directory. getLogicalDrives ();} /// <summary> /// move data /// </summary> /// <param name = "oldPath"> original path </param> /// <param name = "newPath"> New Path </param> // <returns> whether the operation is successful </returns> public bool MoveFile (string oldPath, string newPath) {if (File. exi Sts (oldPath) {try {File. move (oldPath, newPath); return true;} catch (Exception e) {Console. writeLine ("moving file {0}, failed, cause: {1}", oldPath, e. toString (); return false ;}} else {Console. writeLine ("Error, {0} file does not exist !!! ", OldPath); return false ;}} /// <summary> /// copy a file /// </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 ("copying file {0}, failed, cause: {1}", oldPath, e. toString (); return fal Se ;}} else {Console. WriteLine ("Error, {0} file does not exist !!! ", OldPath); return false ;}} /// <summary> /// delete a file /// </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 ("deleting file {0}, failed, cause: {1}", targetPath, e. toString (); return false ;}} else {Console. writeLine ("Error, {0} file does not exist !!! ", TargetPath); return false ;}} /// <summary> /// create a folder /// </summary> /// <param name = "path"> </param> /// <returns> </returns> public bool CreateFolder (string path) {if (Directory. exists (path) {Console. writeLine ("folder {0} already exists", path); return true;} else {try {DirectoryInfo dirInfo = Directory. createDirectory (path); Console. writeLine ("folder created successfully, created at {0}", Directory. getCreationTime (path); r Eturn true;} catch (Exception e) {Console. writeLine ("failed to create folder, cause of failure {0}", e. toString (); return false ;}}} /// <summary> /// delete a folder /// </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 ("failed to delete folder, cause of failure {0}", e. toString (); retu Rn 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 ("moving folder {0}, failed, cause: {1}", oldPath, e. toString ()); Return false ;}} else {Console. WriteLine ("Error, {0} Folder does not exist !!! ", OldPath); return false ;}/// <summary> /// read files (read one by one) outside of the stream, cannot read the correct value /// </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); // set the pointer To start with while (br. baseStream. position <br. baseStream. length) {try {Console. writeLine (br. readString ();} catch (EndOfStreamException e) {Console. writeLine ("has reached the end of {0}", e. toString () ;}} br. close (); fs. close (); return true;} else {return false ;}} /// <summary> /// read text /// </summary> /// <param name = "targetPath"> </param> /// <returns> </returns> public bool ReadCommon (string targetPath) {if (File. exists (targetPath) {// using (StreamReader sr = File. openText (targetPath) // read Chinese characters and garbled using (StreamReader sr = new StreamReader (targetPath, UnicodeEncoding. getEncoding ("GB2312") // solves Chinese garbled characters {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) // garbled Chinese using (StreamWriter sw = new StreamWriter (targetPath, true, UnicodeEncoding. getEncoding ("GB2312") // solves Chinese garbled characters {if (isNendWarp) {sw. writeLine (content);} else {sw. write (content);} sw. close () ;}return true ;}else {return false ;}}}}