您好,登錄后才能下訂單哦!
這篇文章主要介紹C#中文件操作FileInfo類怎么用,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
FileInfo類 提供了與File類相同的功能,不同的是FileInfo提供的都是成員方法
1、讀文件
//摘要:創(chuàng)建只讀 System.IO.FileStream。 public System.IO.FileStream OpenRead() //摘要:創(chuàng)建使用 UTF8 編碼、從現(xiàn)有文本文件中進(jìn)行讀取的 System.IO.StreamReader。 public System.IO.StreamReader OpenText()
2、寫文件
//摘要:創(chuàng)建只寫 System.IO.FileStream。 public System.IO.FileStream OpenWrite()
3、追加內(nèi)容
//摘要:創(chuàng)建一個(gè) System.IO.StreamWriter,它向 System.IO.FileInfo 的此實(shí)例表示的文件追加文本。 public System.IO.StreamWriter AppendText()
4、打開文件
//摘要:在指定的模式中打開文件。 public System.IO.FileStream Open(System.IO.FileMode mode) //摘要:用讀、寫或讀/寫訪問權(quán)限在指定模式下打開文件。 public System.IO.FileStream Open(System.IO.FileMode mode, System.IO.FileAccess access) //摘要:用讀、寫或讀/寫訪問權(quán)限和指定的共享選項(xiàng)在指定的模式中打開文件。 public System.IO.FileStream Open(System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share)
5、復(fù)制、移動(dòng)、替換
//摘要:將現(xiàn)有文件復(fù)制到新文件,不允許覆蓋現(xiàn)有文件。 public System.IO.FileInfo CopyTo(string destFileName) //摘要:將現(xiàn)有文件復(fù)制到新文件,允許覆蓋現(xiàn)有文件。 public System.IO.FileInfo CopyTo(string destFileName, bool overwrite) //摘要:將指定文件移到新位置,并提供指定新文件名的選項(xiàng)。 public void MoveTo(string destFileName) //摘要:使用當(dāng)前 System.IO.FileInfo 對(duì)象所描述的文件替換指定文件的內(nèi)容,這一過程將刪除原始文件,并創(chuàng)建被替換文件的備份。 public System.IO.FileInfo Replace(string destinationFileName, string destinationBackupFileName) //摘要:使用當(dāng)前 System.IO.FileInfo 對(duì)象所描述的文件替換指定文件的內(nèi)容,這一過程將刪除原始文件,并創(chuàng)建被替換文件的備份。還指定是否忽略合并錯(cuò)誤。 public System.IO.FileInfo Replace(string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors)
6、加密解密、刪除
//摘要:將某個(gè)文件加密,使得只有加密該文件的帳戶才能將其解密。 public void Encrypt() //摘要:解密由當(dāng)前帳戶使用 System.IO.FileInfo.Encrypt() 方法加密的文件。 public void Decrypt() //摘要:永久刪除文件。 public override void Delete()
7、獲得文件屬性
//摘要:獲取父目錄的實(shí)例。 public System.IO.DirectoryInfo Directory { get; } //摘要:獲取表示目錄的完整路徑的字符串。 public string DirectoryName { get; } //摘要:獲取指示文件是否存在的值。 public override bool Exists { get; } //摘要:獲取或設(shè)置確定當(dāng)前文件是否為只讀的值。 public bool IsReadOnly { set; get; } //摘要:獲取當(dāng)前文件的大?。ㄗ止?jié))。 public long Length { get; } //摘要:獲取文件名。 public override string Name { get; }
在FileInfo中獲取文件的相關(guān)屬性不再是方法了,都是通過屬性獲得的,并且除是否只讀屬性為可讀可寫的,其他屬性都是只讀的。
以上是“C#中文件操作FileInfo類怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。