{"id":869,"date":"2016-12-13T18:32:54","date_gmt":"2016-12-13T10:32:54","guid":{"rendered":"http:\/\/39.100.100.179\/?p=869"},"modified":"2016-12-13T20:07:03","modified_gmt":"2016-12-13T12:07:03","slug":"system-io%e6%96%87%e4%bb%b6%e6%93%8d%e4%bd%9c","status":"publish","type":"post","link":"http:\/\/www.u3d8.com\/?p=869","title":{"rendered":"System.IO\u6587\u4ef6\u64cd\u4f5c"},"content":{"rendered":"<p>\u8fd9\u4e24\u5929\u4e00\u76f4\u5728\u770bmsdn\u6587\u6863\uff0c\u4eca\u5929\u770b\u5230System.IO\u7684\u4e00\u4e9b\u6559\u7a0b\uff0c\u662f\u5bf9windows\u4e0b\u6587\u4ef6\u6216\u6587\u4ef6\u5939\u7684\u589e\u5220\u6539\u7684\u64cd\u4f5c\u3002<\/p>\n<p>\u4e0b\u9762\u5c31\u628a\u5b66\u5230\u7684\u8fd9\u4e9b\u5206\u4eab\u51fa\u6765\u3002\u5df2\u7ecf\u5199\u597d\u4e86\u6ce8\u91ca\uff0c\u6309\u7167\u6ce8\u91ca\u4e00\u6b65\u6b65\u770b\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<pre class=\"lang:c# decode:true \">using UnityEngine;\r\nusing System.Collections;\r\nusing System.IO;\r\n\r\npublic class Test : MonoBehaviour {\r\n\r\n    void Start()\r\n    {\r\n        CreateFileAndDirector();\r\n\r\n        WriteTextFile();\r\n\r\n        ReadTextFile();\r\n\r\n        CopyAndMoveFile();\r\n    }\r\n\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ \u521b\u5efa\u6587\u4ef6\u5939\u201cMyTest\u201d\u548c\u6587\u4ef6\u201c1.txt\u201d\r\n    \/\/\/ &lt;\/summary&gt;\r\n    void CreateFileAndDirector()\r\n    {\r\n        \/\/ \u6307\u5b9a\u4e00\u4e2a\u201c\u5f53\u524d\u6d3b\u52a8\u6587\u4ef6\u5939\u201d\r\n        string activeDir = @\"d:\\\";\r\n\r\n        \/\/ \u5408\u5e76\u8def\u5f84\u5b57\u7b26\u4e32 \u7ec4\u5408\u6210\uff1ad:\\MyTest\r\n        string newPath = Path.Combine(activeDir, \"MyTest\");\r\n\r\n        \/\/ \u521b\u5efa\u6587\u4ef6\u5939\r\n        Directory.CreateDirectory(newPath);\r\n\r\n        \/\/ \u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\u7684\u540d\u5b57\r\n        string newFileName = \"1.txt\";\r\n\r\n        \/\/ \u6307\u5b9a\u8be5\u6587\u4ef6\u4f4d\u7f6e\r\n        newPath = Path.Combine(newPath, newFileName);\r\n\r\n        \/\/ \u521b\u5efa\u6587\u4ef6\r\n        if (!File.Exists(newPath))\r\n        {\r\n            File.Create(newPath);\r\n        }\r\n\r\n    }\r\n\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ \u5199\u5165\u6587\u4ef6\r\n    \/\/\/ &lt;\/summary&gt;\r\n    void WriteTextFile()\r\n    {\r\n        string path = @\"d:\\MyTest\\1.txt\";\r\n\r\n        string text = \"A class is the most powerful data type in C#. Like structures, \" +\r\n                           \"a class defines the data and behavior of the data type. \";\r\n        \/\/ \u4e0d\u5206\u884c\r\n        File.WriteAllText(path, text);\r\n\r\n        string[] lines = { \"First line\", \"Second line\", \"Third line\" };\r\n        \/\/ \u5199\u5230\u4e09\u884c\r\n        File.WriteAllLines(@\"d:\\MyTest\\1.txt\", lines);\r\n    }\r\n\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ \u8bfb\u53d6\u6587\u4ef6\r\n    \/\/\/ &lt;\/summary&gt;\r\n    void ReadTextFile()\r\n    {\r\n        \/\/ \u8bfb\u53d6\u6240\u6709\u5185\u5bb9\r\n        string text = File.ReadAllText(@\"d:\\MyTest\\1.txt\");\r\n        Debug.Log(text);\r\n\r\n        \/\/ \u5206\u884c\u8bfb\u53d6\u6240\u6709\u5185\u5bb9\r\n        string[] lines = File.ReadAllLines(@\"d:\\MyTest\\1.txt\");\r\n        Debug.Log(lines[0]);\r\n    }\r\n\r\n    \/\/\/ &lt;summary&gt;\r\n    \/\/\/ \u590d\u5236\u548c\u79fb\u52a8\u6587\u4ef6\r\n    \/\/\/ &lt;\/summary&gt;\r\n    void CopyAndMoveFile()\r\n    {\r\n        string fileName = \"1.txt\";\r\n\r\n        string sourcePath = @\"d:\\MyTest\";\r\n        string targetPath = @\"d:\\MyTest\\SubDir\";\r\n\r\n\r\n        string sourceFile = Path.Combine(sourcePath, fileName);\r\n        string destFile = Path.Combine(targetPath, fileName);\r\n\r\n        if (!Directory.Exists(targetPath))\r\n        {\r\n            Directory.CreateDirectory(targetPath);\r\n        }\r\n\r\n        \/\/ \u590d\u5236\u6587\u4ef6\uff0cTRUE\u4e3a\u5982\u679c\u76ee\u6807\u76ee\u5f55\u5df2\u5b58\u5728\u8be5\u6587\u4ef6\uff0c\u5219\u8986\u76d6\uff1bFALSE\u5df2\u5b58\u5728\u8be5\u6587\u4ef6 \u5219\u53d6\u6d88\u590d\u5236\r\n        File.Copy(sourceFile, destFile, true);\r\n\r\n        \/\/ \u79fb\u52a8\u6587\u4ef6\r\n        \/\/File.Move(sourceFile, destFile);\r\n    }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u4e24\u5929\u4e00\u76f4\u5728\u770bmsdn\u6587\u6863\uff0c\u4eca\u5929\u770b\u5230System.IO\u7684\u4e00\u4e9b\u6559\u7a0b\uff0c\u662f\u5bf9windo &hellip; <a href=\"http:\/\/www.u3d8.com\/?p=869\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[24],"tags":[216,217],"_links":{"self":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/869"}],"collection":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=869"}],"version-history":[{"count":2,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/869\/revisions"}],"predecessor-version":[{"id":871,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/869\/revisions\/871"}],"wp:attachment":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=869"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=869"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=869"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}