1. File attributes operation
Both the file class and the FileInfo can be implemented. The difference between a static method and an instantiated Method!
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; Hidden and read-onlyConsolewriteline (fi. Attributes. Tostring//read-only with System properties, remove when prompted to deny access fi attributes = Fileattributes. Archive; console. Writeline (fi. Attributes. Tostring /span>
2. File path
Path operations for files and folders are in the path class. In addition, you can use the Environment class, which contains information about the environment and the program.
StringDirpath= @"D:\TestDir";StringFilePath= @"D:\TestDir\TestFile.txt";Console.WriteLine("<<<<<<<<<<<{0}>>>>>>>>>>", "File path");Get current pathConsole.WriteLine(Environment.CurrentDirectory);The directory where the file or folder residesConsole.WriteLine(Path.GetDirectoryName(FilePath)); D:\TestDirConsole.WriteLine(Path.GetDirectoryName(Dirpath)); D:\File name extensionConsole.WriteLine(Path.GetExtension(FilePath)); . txtFilenameConsole.WriteLine(Path.GetFileName(FilePath)); TestFile.txtConsole.WriteLine(Path.GetFileName(Dirpath)); TestDirConsole.WriteLine(Path.Getfilenamewithoutextension(FilePath)); TestfileAbsolute pathConsole.WriteLine(Path.GetFullPath(FilePath)); D:\TestDir\TestFile.txtConsole.WriteLine(Path.GetFullPath(Dirpath)); D:\TestDirChange extensionConsole.WriteLine(Path.Changeextension(FilePath, ". jpg"));D:\TestDir\TestFile.jpgroot directoryConsole.WriteLine(Path.Getpathroot(Dirpath)); D:\Build pathConsole.writeline (path. Combine (new string[]< Span class= "PLN" > { @ "D:\", " basedirsubdir Testfile. Txt
C # File notes