c# 檔案筆記

來源:互聯網
上載者:User

標籤:返回   目錄   archive   ado   ted   txt   pre   mfile   系統   

1、檔案屬性操作

File類與FileInfo都能實現。靜態方法與執行個體化方法的區別!

//use File classConsole.WriteLine(File.GetAttributes(filePath));File.SetAttributes(filePath,FileAttributes.Hidden | FileAttributes.ReadOnly);Console.WriteLine(File.GetAttributes(filePath));//user FilInfo classFileInfo fi = new FileInfo(filePath);Console.WriteLine(fi.Attributes.ToString());fi.Attributes = FileAttributes.Hidden | FileAttributes.ReadOnly; //隱藏與唯讀Console.WriteLine(fi.Attributes.ToString());//唯讀與系統屬性,刪除時會提示拒絕訪問fi.Attributes = FileAttributes.Archive;Console.WriteLine(fi.Attributes.ToString());

2、檔案路徑

檔案和檔案夾的路徑操作都在Path類中。另外還可以用Environment類,裡麵包含環境和程式的資訊。

string dirPath = @"D:\TestDir";string filePath = @"D:\TestDir\TestFile.txt";Console.WriteLine("<<<<<<<<<<<{0}>>>>>>>>>>", "檔案路徑");//獲得當前路徑Console.WriteLine(Environment.CurrentDirectory);//檔案或檔案夾所在目錄Console.WriteLine(Path.GetDirectoryName(filePath));     //D:\TestDirConsole.WriteLine(Path.GetDirectoryName(dirPath));      //D:\//副檔名Console.WriteLine(Path.GetExtension(filePath));         //.txt//檔案名稱Console.WriteLine(Path.GetFileName(filePath));          //TestFile.txtConsole.WriteLine(Path.GetFileName(dirPath));           //TestDirConsole.WriteLine(Path.GetFileNameWithoutExtension(filePath)); //TestFile//絕對路徑Console.WriteLine(Path.GetFullPath(filePath));          //D:\TestDir\TestFile.txtConsole.WriteLine(Path.GetFullPath(dirPath));           //D:\TestDir  //更改副檔名Console.WriteLine(Path.ChangeExtension(filePath, ".jpg"));//D:\TestDir\TestFile.jpg//根目錄Console.WriteLine(Path.GetPathRoot(dirPath));           //D:\      //產生路徑Console.WriteLine(Path.Combine(new string[] { @"D:\", "BaseDir", "SubDir", "TestFile.txt" })); //D:\BaseDir\SubDir\TestFile.txt//產生隨即檔案夾名或檔案名稱Console.WriteLine(Path.GetRandomFileName());//建立磁碟上唯一命名的零位元組的臨時檔案並返回該檔案的完整路徑Console.WriteLine(Path.GetTempFileName());//返回當前系統的臨時檔案夾的路徑Console.WriteLine(Path.GetTempPath());//檔案名稱中無效字元Console.WriteLine(Path.GetInvalidFileNameChars());//路徑中無效字元Console.WriteLine(Path.GetInvalidPathChars()); 

c# 檔案筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.