1. 程式人生 > >C#基礎(19)——path、File類的基本操作

C#基礎(19)——path、File類的基本操作

1、path類

F12轉到定義,這是靜態類,可以[類名].方法名:
這裡寫圖片描述
以前的方法擷取字串:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Path類
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = @"C:\Users\Administrator\Desktop\test.txt"
; int index = path.LastIndexOf("\\"); string str = path.Substring(index+1); Console.WriteLine(str); Console.ReadKey(); } } }

取資料夾名、副檔名和字尾:

這裡寫圖片描述

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text
; using System.Threading.Tasks; namespace Path類 { class Program { static void Main(string[] args) { string path = @"C:\Users\Administrator\Desktop\test.txt"; string str = Path.GetFileName(path); Console.WriteLine(str); Console.WriteLine
("沒有副檔名:{0}",Path.GetFileNameWithoutExtension(path)); Console.WriteLine("取副檔名:{0}", Path.GetExtension(path)); Console.WriteLine("資料夾名路徑:{0}", Path.GetDirectoryName(path)); Console.WriteLine("全路徑:{0}", Path.GetFullPath(path)); Console.WriteLine("組合路徑:{0}", Path.Combine(path,"my.txt")); Console.ReadKey(); } } }

2、File類

用於建立、寫入檔案等,如建立:

 string path = @"C:\Users\Administrator\Desktop\test.txt";
            File.Create(path);
            Console.WriteLine("建立成功!");//已存在,會被覆蓋
            Console.ReadKey();

刪除檔案:

 string path = @"C:\Users\Administrator\Desktop\test.txt";
            File.Delete(path);
            Console.WriteLine("刪除成功!");//徹底刪除,不能恢復
            Console.ReadKey();

拷貝檔案:

string path = @"C:\Users\Administrator\Desktop\test.txt";
            File.Create(path);//出問題:
            //其他資訊: 檔案“C:\Users\Administrator\Desktop\test.txt”正由另一程序使用,因此該程序無法訪問此檔案。
            File.Copy(path, @"C:\Users\Administrator\Desktop\test1.txt");
            Console.ReadKey();

3、檔案的讀取

去讀取行,只能讀取小檔案,因為它是讀取全部到記憶體,返回string 的陣列:

 string[] contains=File.ReadAllLines(path);
            for (int i = 0; i < contains.Length; i++)
            {
                Console.WriteLine(contains[i]);
            }

用ReadAllText,返回string:

string path = @"C:\Users\Administrator\Desktop\test.txt";
            string str = File.ReadAllText(path);
            Console.WriteLine(str);

以上只用於處理文字,當為視訊影象時:

string path = @"C:\Users\Administrator\Desktop\test.txt";
            byte[] btys = File.ReadAllBytes(path);
            //將位元組陣列中的每一個元素按照我們指定的編碼格式解碼成字串
            //utf-8 GB2312 GBK  ASCII Unicode
            string s = Encoding.Default.GetString(btys);
            Console.WriteLine(s);

4、檔案的寫入

以位元組形式:

string path = @"C:\Users\Administrator\Desktop\new.txt";
            string str = "今天天氣好晴朗,處處好風光!";
            byte[] buffer = Encoding.Default.GetBytes(str);
            File.WriteAllBytes(path, buffer);
            Console.WriteLine("Write Down");

以WriteAllLine形式:

string path = @"C:\Users\Administrator\Desktop\new.txt";
            File.WriteAllLines(path, new string[] { "111", "222", "333", "444" });
            Console.WriteLine("Write Down");

以WriteAllText

string path = @"C:\Users\Administrator\Desktop\new.txt";
            File.WriteAllText(path, "111\r\n2222\r\n333");
            Console.WriteLine("Write Down");

5、檔案的追加

檔案的追加,不會覆蓋和刪除原檔案,相當於Python的with open(‘haproxy’, ‘a’, encoding=’utf-8’) as f1:
方法1:

string path = @"C:\Users\Administrator\Desktop\new.txt";
            File.AppendAllText(path, "我是新來的。");
            Console.WriteLine("Write Down");

方法2:

 string path = @"C:\Users\Administrator\Desktop\new.txt";
            File.AppendAllLines(path,new string[] {"aaaaa"});
            Console.WriteLine("Write Down");

6、絕對路徑和相對路徑

絕對路徑:通過電腦給定的完整路徑
相對路徑:可執行路徑下的路徑“new.txt”,bin→debug下
所以我們在開發時儘量使用相對路徑

7、檔案流

解決讀取內容過小問題,處理大檔案