標籤:返回 目錄 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# 檔案筆記