国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

C# 文件操作(復(fù)制、移動、重命名、創(chuàng)建、打開、刪除)

這篇具有很好參考價值的文章主要介紹了C# 文件操作(復(fù)制、移動、重命名、創(chuàng)建、打開、刪除)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

目錄

一、簡介

二、創(chuàng)建文件

三、寫入文件

四、讀取文件

五、復(fù)制文件

六、移動文件

七、重命名文件

八、刪除文件

結(jié)束


一、簡介

C#中的IO(Input/Output)操作包括讀取和寫入文件、讀取和寫入流、以及操作目錄和文件夾等。這些操作都可以通過System.IO命名空間中的類實現(xiàn)。下面對C# IO相關(guān)的類和操作進(jìn)行介紹。

文件操作
File類
File類提供了對文件的創(chuàng)建、讀取、寫入、復(fù)制、移動、重命名和刪除等操作。

StreamReader和StreamWriter類
StreamReader和StreamWriter類用于讀取和寫入文本文件。

目錄和文件夾操作
Directory和DirectoryInfo類
Directory和DirectoryInfo類提供了對目錄和文件夾的創(chuàng)建、移動、重命名和刪除等操作。

二、創(chuàng)建文件

要創(chuàng)建一個文件,可以使用System.IO.File.Create()方法。該方法接受一個文件路徑和一個可選的緩沖區(qū)大小作為參數(shù),然后返回一個FileStream對象,該對象可用于寫入文件。

代碼:

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

namespace 文件操作
{
    internal class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string filePath = @"E:\myFile.txt";
                FileStream fileStream = File.Create(filePath);
                fileStream.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("創(chuàng)建文件失?。? + ex.Message);
            }
            Console.WriteLine("創(chuàng)建文件成功!");
            Console.ReadKey();
        }
    }
}

打開E盤,文件是創(chuàng)建成功了,只是這個 txt 文件里沒有任何內(nèi)容

C# 文件操作(復(fù)制、移動、重命名、創(chuàng)建、打開、刪除)

三、寫入文件

寫入文件可以使用下面方式:
1.?File.WriteAllText(FilePath,String)
2.?File.WriteAllText(FilePath, String,Encoding) ----指定編碼
3.?File.WriteAllLines(FilePath,String[])
4.?File.WriteAllLines(FilePath,String[],Encoding) ----指定編碼

前面兩種寫入的是一個字符串,后面兩種寫入的是一個字符串?dāng)?shù)組。

使用 File.WriteAllText 或 File.WriteAllLines 方法時,如果指定的文件路徑不存在,會創(chuàng)建一個新文件;如果文件已經(jīng)存在,則會覆蓋原文件。

第一種:字符串的寫入

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

namespace 文件操作
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string filePath = @"E:\myFile.txt";
            string content = "讀取讀取讀取讀取讀取讀取";
            //第一種
            //File.WriteAllText(filePath, content);
            //第二種
            File.WriteAllText(filePath, content, Encoding.UTF8);

            Console.ReadKey();
        }
    }
}

打開剛創(chuàng)建的 myFile.txt 文件:

C# 文件操作(復(fù)制、移動、重命名、創(chuàng)建、打開、刪除)

第二種:多行字符串的寫入

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

namespace 文件操作
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string filePath = @"E:\myFile.txt";
            string[] contentArr = { "dddd", "eeeeee" };

            //第一種
            //File.WriteAllLines(filePath, contentArr);
            //第二種
            File.WriteAllLines(filePath, contentArr, Encoding.UTF8);

            Console.ReadKey();
        }
    }
}

上面寫入的字符串現(xiàn)在已經(jīng)被覆蓋了

C# 文件操作(復(fù)制、移動、重命名、創(chuàng)建、打開、刪除)

上面寫入字符串都會覆蓋原有的內(nèi)容,下面就介紹如何追加字符串。

第三種:追加字符串

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

namespace Test1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string filePath = @"E:\myFile.txt";
            string content = "423423423423";

            File.AppendAllText(filePath, content);

            Console.ReadKey();
        }
    }
}

filePath 路徑這個文件如果沒有,C# 會自動生成這個文件,運行后找到這個文件,就可以看到添加的內(nèi)容

C# 文件操作(復(fù)制、移動、重命名、創(chuàng)建、打開、刪除)

下面我將內(nèi)容做一下更改,再次運行

namespace Test1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string filePath = @"E:\myFile.txt";
            string content = "dffsddgaadfasdfa";

            File.AppendAllText(filePath, content);

            Console.ReadKey();
        }
    }
}

我們重寫打開?myFile.txt 這個文件,發(fā)現(xiàn)文字是追加上去了,但是并沒有換行

C# 文件操作(復(fù)制、移動、重命名、創(chuàng)建、打開、刪除)

其實換行也非常的簡單,直接在字符串里加轉(zhuǎn)義字符 \n 就行了

namespace Test1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string filePath = @"E:\myFile.txt";
            string content = "\n哇哈哈哈哈";

            File.AppendAllText(filePath, content);

            Console.ReadKey();
        }
    }
}

效果:

C# 文件操作(復(fù)制、移動、重命名、創(chuàng)建、打開、刪除)

第四種:追加多行字符串

按上面的套路,添加多行當(dāng)然也是可以的,不過用的方法名要換一個了

使用?File.AppendAllLines 來添加多行文字。

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

namespace Test1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string filePath = @"E:\myFile.txt";
            string[] list = { "\n恭喜", "發(fā)財", "紅包", "拿來" };
            File.AppendAllLines(filePath, list, Encoding.UTF8);

            Console.ReadKey();
        }
    }
}

運行后:

C# 文件操作(復(fù)制、移動、重命名、創(chuàng)建、打開、刪除)

四、讀取文件

讀取文件可以使用下面方式:

1.File.ReadAllText(FilePath)
2.File.ReadAllText(FilePath, Encoding) ----指定編碼
3.File.ReadAllLines(FilePath)
4.File.ReadAllLines(FilePath, Encoding) ----指定編碼

以字符串接收方式

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

namespace 文件操作
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string path = @"E:\myFile.txt";
            //string content = File.ReadAllText(path);
            string content = File.ReadAllText(path,Encoding.UTF8);
            Console.WriteLine(content);
            Console.ReadKey();
        }
    }
}

以字符串?dāng)?shù)組接收的方式

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

namespace 文件操作
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string path = @"E:\myFile.txt";
            //string[] content = File.ReadAllLines(path); 
            string[] content = File.ReadAllLines(path, Encoding.UTF8);
            for (int i = 0; i < content.Length; i++)
            {
                Console.WriteLine(content[i]);
            }
            Console.ReadKey();
        }
    }
}

采用流(Stream)的方式來讀取內(nèi)容

1.StreamReader(FilePath)
2.StreamReader(FilePath, Encoding)
3.StreamReader(FileStream)
4.StreamReader(FileStream, Encoding)
5.File.OpenText(FilePath)
6.FileInfo.OpenText()

基于StreamReader,一行一行讀取

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

namespace 文件操作
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string path = @"E:\myFile.txt";
            //StreamReader sr1 = new StreamReader(path); 
            //StreamReader sr2 = new StreamReader(path, Encoding.UTF8);
            

            //初始化FileStream
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None); 
            //StreamReader sr3 = new StreamReader(fs); 
            StreamReader sr4 = new StreamReader(fs, Encoding.UTF8);

            string line = string.Empty;
            while((line = sr4.ReadLine()) != null)
            {
                Console.WriteLine(line);
            }
            sr4.Close();
            
            Console.ReadKey();
        }
    }
}

一次性讀完

string path = @"E:\myFile.txt";
StreamReader sr = new StreamReader(path, Encoding.UTF8);
string content = sr.ReadToEnd();
Console.WriteLine(content);
sr.Close();

五、復(fù)制文件

要復(fù)制文件,可以使用System.IO.File.Copy()方法。該方法接受源文件路徑和目標(biāo)文件路徑作為參數(shù),并將源文件復(fù)制到目標(biāo)文件。如果目標(biāo)文件已存在,則將被覆蓋。

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

namespace 文件操作
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string sourceFilePath = @"E:\myFile.txt";
            string destinationFilePath = @"E:\myFile_copy.txt";
            File.Copy(sourceFilePath, destinationFilePath);

            Console.ReadKey();
        }
    }
}

打開對應(yīng)的目錄,可以看到拷貝成功了,而且字節(jié)數(shù)也是一樣的

C# 文件操作(復(fù)制、移動、重命名、創(chuàng)建、打開、刪除)

六、移動文件

要移動文件,可以使用System.IO.File.Move()方法。該方法接受源文件路徑和目標(biāo)文件路徑作為參數(shù),并將源文件移動到目標(biāo)文件。如果目標(biāo)文件已存在,則將被覆蓋。

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

namespace 文件操作
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string sourceFilePath = @"E:\myFile.txt";
            string destinationFilePath = @"D:\myFile.txt";
            File.Move(sourceFilePath, destinationFilePath);

            Console.ReadKey();
        }
    }
}

七、重命名文件

要重命名文件,可以使用System.IO.File.Move()方法。該方法接受源文件路徑和目標(biāo)文件路徑作為參數(shù),并將源文件重命名為目標(biāo)文件。如果目標(biāo)文件已存在,則將被覆蓋。

在上一節(jié) 移動文件 中,我們將 myFile.txt 文件移動到 D 盤了,執(zhí)行下面操作之前,要先剪切回來再執(zhí)行操作哦

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

namespace 文件操作
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string filePath = @"E:\myFile.txt";
            string newFilePath = @"E:\myFile_new.txt";
            File.Move(filePath, newFilePath);

            Console.ReadKey();
        }
    }
}

運行后,可以看到效果了

C# 文件操作(復(fù)制、移動、重命名、創(chuàng)建、打開、刪除)

?C# 文件操作(復(fù)制、移動、重命名、創(chuàng)建、打開、刪除)

八、刪除文件

要刪除文件,可以使用System.IO.File.Delete()方法。該方法接受文件路徑作為參數(shù),并將該文件從磁盤上刪除。

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

namespace 文件操作
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string newFilePath = @"E:\myFile_new.txt";
            File.Delete(newFilePath);

            Console.ReadKey();
        }
    }
}

結(jié)束

如果這個帖子對你有所幫助,歡迎 關(guān)注 + 點贊 + 留言

end文章來源地址http://www.zghlxwxcb.cn/news/detail-429892.html

到了這里,關(guān)于C# 文件操作(復(fù)制、移動、重命名、創(chuàng)建、打開、刪除)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 解決打開文件、文件夾、拖拽復(fù)制刪除時鼠標(biāo)卡頓

    解決打開文件、文件夾、拖拽復(fù)制刪除時鼠標(biāo)卡頓

    最近新?lián)Q了電腦,一開始沒有問題,但是用了一段時間后又出現(xiàn)了鼠標(biāo)卡頓(說明這問題不是因為電腦性能太差),最后參考如下文章解決問題【學(xué)習(xí)筆記】記錄一個win 11 操作文件卡頓,Windows 資源管理器CPU占用飆升問題_shexview-x64-CSDN博客 原來是百度網(wǎng)盤惹的禍 我這個方法

    2024年02月11日
    瀏覽(23)
  • 刪除、移動、復(fù)制文件時總是要卡在99%一段時間解決方法

    刪除、移動、復(fù)制文件時總是要卡在99%一段時間解決方法

    Win10文件夾重命名、移動、刪除等操作卡頓3-5秒。 原因分析: 查看發(fā)現(xiàn),卡頓期間資源管理器無響應(yīng),并且其高度占用CPU資源,但是對于非文件夾文件操作沒有問題。 解決方案: 1、雙擊“此電腦”,選擇“查看”,再選擇“選項”; 2、依次選擇“常規(guī)”–“清除”–“還原

    2024年02月10日
    瀏覽(21)
  • Linux對文件夾操作(復(fù)制,移動)

    Linux對文件夾操作(復(fù)制,移動)

    將vue 文件夾下面的所有文件,復(fù)制到同目錄下vue-copy文件夾下面 -a:相當(dāng)于 -d、-p、-r 選項的集合,這幾個選項我們一一介紹; -d:如果源文件為軟鏈接(對硬鏈接無效),則復(fù)制出的目標(biāo)文件也為軟鏈接; -i:詢問,如果目標(biāo)文件已經(jīng)存在,則會詢問是否覆蓋; -l:把目標(biāo)文

    2024年02月15日
    瀏覽(21)
  • Python 文件處理指南:打開、讀取、寫入、追加、創(chuàng)建和刪除文件

    文件處理是任何Web應(yīng)用程序的重要部分。Python有多個用于創(chuàng)建、讀取、更新和刪除文件的函數(shù)。 在Python中處理文件的關(guān)鍵函數(shù)是open()函數(shù)。open()函數(shù)接受兩個參數(shù):文件名和模式。 有四種不同的方法(模式)可以打開文件: \\\"r\\\" - 讀取 - 默認(rèn)值。打開一個文件以進(jìn)行讀取,如

    2024年02月05日
    瀏覽(17)
  • C#怎樣創(chuàng)建、移動及遍歷文件夾

    C#怎樣創(chuàng)建、移動及遍歷文件夾

    一、使用DirectoryInfo類創(chuàng)建文件夾: 1、使用DirectoryInfo前需要引入命名空間: 2、DirectoryInfo類沒有靜態(tài)方法,僅可以用于實例化的對象, ?3、判斷輸入的文件夾名稱是否為空,彈出提示框 4、 通過Exists()方法判斷要創(chuàng)建的文件夾是否存在 5、創(chuàng)建文件夾: ?二、使用DirectoryI

    2024年02月12日
    瀏覽(27)
  • c# 操作剪切板,復(fù)制文本或文件

    ?1.將文本內(nèi)容放入剪切板?? ? ? ? ?Clipboard.SetDataObject(\\\"要復(fù)制的內(nèi)容\\\");//復(fù)制內(nèi)容到粘貼板 2.將文本內(nèi)容從剪切板取出?? ? ? ? ? ? ? IDataObject iData = Clipboard.GetDataObject(); ? ? ? ? ? if (iData.GetDataPresent(DataFormats.Text)) ? ? ? ? ? ?{ ? ? ? ?? ? label1.Text = (String)iData.GetData(D

    2023年04月08日
    瀏覽(18)
  • Android文件基本操作(創(chuàng)建文件(夾)、復(fù)制文件(夾)、設(shè)置文件訪問權(quán)限)

    Android文件基本操作(創(chuàng)建文件(夾)、復(fù)制文件(夾)、設(shè)置文件訪問權(quán)限)

    將src目錄下的info.txt復(fù)制到dst目錄并重命名為info_dst.txt 1、 方法一:調(diào)用java.nio.file.Files.copy() 2、方法二:使用輸入輸出流 1、刪除文件 只需要調(diào)用File的delete方法即可刪除指定文件 2、刪除文件夾 如果文件夾不為空,調(diào)用delete方法是無法刪除文件夾的。需要先刪除文件夾中包含

    2024年02月01日
    瀏覽(27)
  • 空文件夾刪不掉打不開,“該項目不存在請確認(rèn)該項目位置“,“項目正在打開中無法刪除“,“文件已損壞或者已經(jīng)被移動刪除“(多種方法圖文詳解,細(xì)節(jié)需要注意,以及可能遇到的問題)

    空文件夾刪不掉打不開,“該項目不存在請確認(rèn)該項目位置“,“項目正在打開中無法刪除“,“文件已損壞或者已經(jīng)被移動刪除“(多種方法圖文詳解,細(xì)節(jié)需要注意,以及可能遇到的問題)

    這個刪不掉的文件或文件夾其實是Windows系統(tǒng)的祖?zhèn)鱞ug到目前為止依然沒有修復(fù),所以說我們需要通過特別的手段來處理它,聽我慢慢講他的緣由可能會對解決這個問題的幫助更多,會有幾種方法,我都試過了的,我把最后一種成功的放在第一個講,沒有成功的可以參考。 事情緣由因

    2024年02月06日
    瀏覽(95)
  • Linux | 創(chuàng)建 | 刪除 | 查看 | 基本命名詳解

    Linux | 創(chuàng)建 | 刪除 | 查看 | 基本命名詳解

    本門課程學(xué)習(xí)Linux系統(tǒng)編程,你可能要問Linux從哪里來?它是怎么發(fā)展的?在這里簡要介紹Linux的發(fā)展史。要說Linux,還得從UNIX說起。 下面內(nèi)容來自維基百科 Linux是一種自由和開放源碼的類UNIX操作系統(tǒng)。該操作系統(tǒng)的內(nèi)核由林納斯·托瓦茲在1991年10月5日首次發(fā)布,再加上用戶

    2024年02月05日
    瀏覽(26)
  • 初識Unity——創(chuàng)建代碼、場景以及五個常用面板(創(chuàng)建C#代碼、打開代碼文件、場景的創(chuàng)建、Project、Hierarchy、Inspector、Scene、Game )

    初識Unity——創(chuàng)建代碼、場景以及五個常用面板(創(chuàng)建C#代碼、打開代碼文件、場景的創(chuàng)建、Project、Hierarchy、Inspector、Scene、Game )

    目錄 創(chuàng)建代碼 創(chuàng)建C#腳本 打開代碼文件 可能出現(xiàn)的問題 場景 場景的創(chuàng)建 基本介紹 五個窗口面板的作用 Project Hierarchy Inspector Scene Game? 從unity2018版本開始,unity就開始不再維護(hù)和推薦JavaScript for Unity以及Boo等語言,現(xiàn)在官方主推和最常用的腳本語言是C#。 創(chuàng)建一個腳本之后

    2024年02月07日
    瀏覽(37)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包