C# IO流 File.Exists,Directory.Exists, File.Create,Directory.CreateDirectory


 1     void Start()
 2     {
 3         CreateDirectory();
 4         CreateFile();
 5     }
 6 
 7     //平台的路徑(封裝起來的一個屬性,這不是一個方法)
 8     public string path
 9     {
10         get
11         {
12             //pc,ios //android :jar:file//
13             //Application.persistentDataPath: C:/Users/電腦的用戶名/AppData/LocalLow/CompanyName/ProductName
14             return Application.persistentDataPath + "/CacheDirectory/";
15         }
16     }
17 
18     //創建目錄
19     void CreateDirectory()
20     {
21         //if (!File.Exists(path + "a.txt")) //判斷某個目錄下是否存在某個文件
22         if (!Directory.Exists(path))  //判斷是否存在某個文件夾
23         {
24             //File.Create(path + "picture.txt");  。
25             Directory.CreateDirectory(path);    //創建文件夾
26             print("CacheDirectory 創建成功!");
27         }
28         else
29         {
30             print("CacheDirectory 已經存在!");
31         }
32     }
33 
34     //創建文件
35     void CreateFile()
36     {
37         //如果不存在這個文件就創建
38         if (!File.Exists(path + "123.txt"))
39         {
40             //在某個文件夾里面創建 .txt/.jpg 等文件,創建不了文件夾,沒有path這個文件夾的話就創建不了(會報錯)
41             File.Create(path + "123.txt");
42             print("123.txt 創建成功!");
43         }
44         else
45         {
46             print("123.txt 已經存在!");
47         }
48 
49         if (!File.Exists(path + "picture.jpg"))
50         {
51             //在某個文件夾里面創建 .txt/.jpg 等文件,創建不了文件夾,沒有path這個文件夾的話就創建不了(會報錯)
52             File.Create(path + "picture.jpg");
53             print("picture.jpg 創建成功!");
54         }
55         else
56         {
57             print("picture.jpg 已經存在!");
58         }
59     }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM