C file operation function creates a file to judge existence

  • 2021-10-11 19:19:24
  • OfStack

File creation:

File.Create(Application.StartupPath + "\\AlarmSet.txt");// Create the file  
System.IO.Path.GetFileName(filePath)       // Returns a file name with an extension
System.IO.Path.GetFileNameWithoutExtension(filePath)     // Returns a file name without extension
System.IO.Path.GetDirectoryName(filePath)     // Return to the directory where the file is located

File exists:

File.Exists(Application.StartupPath + "\\AlarmSet.txt")  
System.IO.DirectoryInfo info = new System.IO.DirectoryInfo(Application.StartupPath + "\\AlarmSet.txt"); 
MessageBox.Show(info.Exists.ToString());

Is it very simple, but sometimes we may feel unfamiliar with functions like GetFileNameWithoutExtension or GetDirectoryName. Before, we always wrote functions to realize them silly, and the original one-line code was done ~


Related articles: