C # File Management

Source: Internet
Author: User

File management focus on two, one is to manage the path of the file is the Management folder is the path class and Directory column class, two classes need to introduce a namespace--using System.IO;

--File path operation

The path operation is relatively simple, basically is to obtain the file path of some methods, these methods exist in a path class, is a static class, then the inside of the method is certainly static method, how to call more needless to say.
Come on, go straight to the example.

//定义一个路径在做例子string str =  @"E:\Study\C#\code\_01path\Program.cs";

1. Get the file name, including the extension Program.cs

Path.GetFileName(str);

2. Get the file name without the extension program

Path.GetFileNameWithoutExtension(str);

3. Get the file extension. cs

Path.GetExtension(str);

4. Get the folder where the files are located e:\study\c#\code_01path

Path.GetDirectoryName(str);

5, get the file full path E:\study\c#\code_01path\program.cs

Path.GetFullPath(str);

6. Connect two strings as a path C:\a\b.txt

Path.Combine(@"C:\a\","b.txt");

File path operations This is the only thing that's common.

--directory manipulating folders and file directories

This thing is also a static class, how to play should know, the following list some common methods

1. Create a folder named New in the specified path

Directory.CreateDirectory(@"C:\Users\Blue\Desktop\new");

2. Delete an empty folder

Directory.Delete(@"C:\Users\Blue\Desktop\new");//这玩意儿是彻底删除,回收站中都不会有

3. Delete a folder with files

//删除带有文件的文件夹是,需要我们确认是否要删除,需要传递第二个bool参数Directory.Delete(@"C:\Users\Blue\Desktop\new",true);

4. Cut Folder

Directory.Move(@"C:\Users\Blue\Desktop\new",@"C:\Users\Blue\Desktop\new1")//第一个参数为原路径,第二个参数为新路径

5. Get the full path of all files in the specified folder, return a string[]

string[] path = Directory.GetFiles(@"C:\Users\Blue\Desktop\examples");

6. Get the full file path for the specified suffix in the specified folder

string[] path = Directory.GetFiles(@"C:\Users\Blue\Desktop\examples","*.html");//就是在GetFiles中添加第二个参数指定

7. Get the full path of all folders within the specified folder

string[] path = Directory.GetDirectories(@"C:\Users\Blue\Desktop\new1");

8. Determine if the specified folder exists

Directory.Exists(@"C:\Users\Blue\Desktop\new1");

C # File Management

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.