C Programming Realization Obtains File Names of All Files in Folder

  • 2021-08-28 20:53:38
  • OfStack

This article describes the example of C # programming implementation to obtain the file names of all files in the folder. Share it for your reference, as follows:

Want to achieve such a function: batch modify the file names of all jpg files in 1 directory, and add specific strings to their original basis

I first store all the file names in the format of jpg in a directory into an array, and then modify the array value to modify the file names of jpg files in batches

The code is as follows:


using System;
using System.IO;
namespace ConsoleApplication7
{
 /// <summary>
 /// Class1  A summary description of. 
 /// </summary>
 class Class1
 {
 /// <summary>
 ///  The main entry point for the application. 
 /// </summary>
 [STAThread]
 static void Main(string[] args)
 {
  string dirp=@"d:\\d";
  DirectoryInfo mydir = new DirectoryInfo(dirp);
  foreach (FileSystemInfo fsi in mydir.GetFileSystemInfos())
  {
  if (fsi is FileInfo)
  {
    FileInfo fi = (FileInfo)fsi;
    string x=System.IO.Path.GetDirectoryName(fi.FullName);
    Console.WriteLine(x);
    string s=System.IO.Path.GetExtension(fi.FullName);
    string y=System.IO.Path.GetFileNameWithoutExtension(fi.FullName);
    Console.WriteLine(y);
    if(s==".jpg")
    {
      System.IO.File.Copy(fi.FullName,x+@"\oo"+fi.Name); // Prefix the original file name with OO
      System.IO.File.Delete(fi.FullName);
    }
  }
  }
  Console.WriteLine(" Success ");
  Console.ReadLine();
 }
 }
}

I hope this article is helpful to everyone's C # programming.


Related articles: