How to Use the C # code to find whether a file is included in a path,

Source: Internet
Author: User

How to Use the C # code to find whether a file is included in a path,

 
String path = "f :\\ haha"; if (Directory. exists (path) {MessageBox. show ("folder"); string filename = path + "\" + "CeShi. xml "; // be sure to add \ if (File. exists (filename) {MessageBox. show ("file exists");} else {MessageBox. show ("File does not exist"); File. create (filename); // Create a file} else {MessageBox. show ("Folder does not exist"); Directory. createDirectory (path); // create path}

 

The following code snippets can be freely combined:
 
01 // determine the existence, creation, and deletion of folders
02 string path = "F: \ haha"; // the correct path
03 if (Directory. Exists (path) // create a file folder if it does not exist
04 {
05 MessageBox. Show ("folder ");

06 // Directory. Delete (path, false); // if there is a file or Directory in the folder, an error is reported here
07 // Directory. Delete (path, true); // true indicates deleting folders and Their subdirectories and files

08}
09 else
10 {
11 MessageBox. Show ("Folder does not exist ");

12 Directory. CreateDirectory (path); // create the folder
13}
14
15 // determine the existence, creation, and deletion of an object
16 string filename = path + "\" + "11.txt ";
17 if (File. Exists (filename ))
18 {
19 MessageBox. Show ("file exists ");
20 File. Delete (filename); // Delete the File
21}
22 else
23 {
24 MessageBox. Show ("file does not exist ");
25 File. Create (filename); // Create the File. If the path Folder does not exist, an error is returned.
26}

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.