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}