C # File Notes

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.