Tool classes for common operation methods on File files in C#

scene

Use of common read and write file methods for File classes in C#

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/99693983

Note:

Blog Home Page:
https://blog.csdn.net/badao_liumang_qizhi
Focus on Public Number
Domineering program ape
Get programming-related e-books, tutorial pushes, and free downloads.

Realization

Get the extension of the file

        /// <summary>
        /// Get the extension of the file
        /// </summary>
        /// <param name="filename">Full filename</param>
        /// <returns>Return extension</returns>
  public static string GetPostfixStr(string filename)
  {
   int num = filename.LastIndexOf(".");
   int length = filename.Length;
   return filename.Substring(num, length - num);
  }

 

Read File Content

  

      /// <summary>
        /// Read File Content
        /// </summary>
        /// <param name="path">File path to read</param>
        /// <returns>Return file contents</returns>
  public static string ReadFile(string path)
  {
   string result;
   if (!System.IO.File.Exists(path))
   {
    result = "No corresponding directory exists";
   }
   else
   {
    System.IO.FileStream stream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);
    System.IO.StreamReader streamReader = new System.IO.StreamReader(stream, System.Text.Encoding.Default);
    result = streamReader.ReadToEnd();
    streamReader.Close();
    streamReader.Dispose();
   }
   return result;
  }

 

Specify encoding format to read file contents

   

     /// <summary>
        /// Read File Content
        /// </summary>
        /// <param name="path">File path to read</param>
        /// <param name="encoding">Encoding Format</param>
        /// <returns>Return file contents</returns>
        public static string ReadFile(string path, System.Text.Encoding encoding)
        {
            string result;
            if (!System.IO.File.Exists(path))
            {
                result = "No corresponding directory exists";
            }
            else
            {
                System.IO.FileStream stream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);
                System.IO.StreamReader streamReader = new System.IO.StreamReader(stream, encoding);
                result = streamReader.ReadToEnd();
                streamReader.Close();
                streamReader.Dispose();
            }
            return result;
        }

 

Write to specified file

  

      /// <summary>
        /// Write to specified file
        /// </summary>
        /// <param name="path">Full path to file to write content</param>
        /// <param name="content">What to write</param>
  public static void WriteFile(string path, string content)
  {
   try
   {
    object obj = new object();
    if (!System.IO.File.Exists(path))
    {
     System.IO.FileStream fileStream = System.IO.File.Create(path);
     fileStream.Close();
    }
    lock (obj)
    {
     using (System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(path, false, System.Text.Encoding.Default))
     {
      streamWriter.WriteLine(content);
      streamWriter.Close();
      streamWriter.Dispose();
     }
    }
   }
   catch (System.Exception ex)
   {
                ICSharpCode.Core.LoggingService<FileHelper>.Error(String.Format("write file{0}abnormal:{1}", path, ex.Message), ex);
   }
  }

 

Specify encoding format to write to file

 

       /// <summary>
        /// Write to specified file
        /// </summary>
        /// <param name="path">Full path to file to write content</param>
        /// <param name="content">What to write</param>
        /// <param name="encoding">Encoding Format</param>
        public static void WriteFile(string path, string content, System.Text.Encoding encoding)
        {
            try
            {
                object obj = new object();
                if (!System.IO.File.Exists(path))
                {
                    System.IO.FileStream fileStream = System.IO.File.Create(path);
                    fileStream.Close();
                }
                lock (obj)
                {
                    using (System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(path, false, encoding))
                    {
                        streamWriter.WriteLine(content);
                        streamWriter.Close();
                        streamWriter.Dispose();
                    }
                }
            }
            catch (System.Exception ex)
            {
                ICSharpCode.Core.LoggingService<FileHelper>.Error(String.Format("write file{0}abnormal:{1}", path, ex.Message), ex);
            }
        }

 

 

File Copy

        /// <summary>
        /// File Copy
        /// </summary>
        /// <param name="orignFile">Full path to source file</param>
        /// <param name="newFile">Full path to target file</param>
  public static void FileCoppy(string orignFile, string newFile)
  {
   System.IO.File.Copy(orignFile, newFile, true);
  }

 

File Delete

 

       /// <summary>
        /// Delete Files
        /// </summary>
        /// <param name="path">Full path of file to delete</param>
  public static void FileDel(string path)
  {
   System.IO.File.Delete(path);
  }

 

file move

 

       /// <summary>
        /// File Move (Clip)->Paste)
        /// </summary>
        /// <param name="orignFile">Full path to source file</param>
        /// <param name="newFile">Full path to target file</param>
  public static void FileMove(string orignFile, string newFile)
  {
   System.IO.File.Move(orignFile, newFile);
  }

 

Determine if a set of files exists

   

     /// <summary>
        /// Determine if a set of files exists
        /// </summary>
        /// <param name="filePathList">File Path List</param>
        /// <returns>Does the file all exist</returns>
        public static bool IsFilesExist(List<string> filePathList)
        {
            bool isAllExist = true;
            foreach(string filePath in filePathList)
            {
                if(!File.Exists(filePath))
                {
                    isAllExist = false;
                }
            }
            return isAllExist;
        }

 

Create directory

 

       /// <summary>
        /// Create directory
        /// </summary>
        /// <param name="orignFolder">current directory</param>
        /// <param name="newFloder">Directory name to create</param>
  public static void FolderCreate(string orignFolder, string newFloder)
  {
   System.IO.Directory.SetCurrentDirectory(orignFolder);
   System.IO.Directory.CreateDirectory(newFloder);
  }

 

Delete Directory

   

     /// <summary>
        /// Delete Directory
        /// </summary>
        /// <param name="dir">Directory to delete</param>
  public static void DeleteFolder(string dir)
  {
   if (System.IO.Directory.Exists(dir))
   {
    string[] fileSystemEntries = System.IO.Directory.GetFileSystemEntries(dir);
    for (int i = 0; i < fileSystemEntries.Length; i++)
    {
     string text = fileSystemEntries[i];
     if (System.IO.File.Exists(text))
     {
      System.IO.File.Delete(text);
     }
     else
     {
      FileHelper.DeleteFolder(text);
     }
    }
    System.IO.Directory.Delete(dir);
   }
  }

 

Directory Content Copy

   

     /// <summary>
        /// Directory Content Copy
        /// </summary>
        /// <param name="srcPath">Source Directory</param>
        /// <param name="aimPath">Target Directory</param>
  public static void CopyDir(string srcPath, string aimPath)
  {
   try
   {
    if (aimPath[aimPath.Length - 1] != System.IO.Path.DirectorySeparatorChar)
    {
     aimPath += System.IO.Path.DirectorySeparatorChar;
    }
    if (!System.IO.Directory.Exists(aimPath))
    {
     System.IO.Directory.CreateDirectory(aimPath);
    }
    string[] fileSystemEntries = System.IO.Directory.GetFileSystemEntries(srcPath);
    string[] array = fileSystemEntries;
    for (int i = 0; i < array.Length; i++)
    {
     string text = array[i];
     if (System.IO.Directory.Exists(text))
     {
      FileHelper.CopyDir(text, aimPath + System.IO.Path.GetFileName(text));
     }
     else
     {
      System.IO.File.Copy(text, aimPath + System.IO.Path.GetFileName(text), true);
     }
    }
   }
   catch (System.Exception ex)
   {
    throw new System.Exception(ex.ToString());
   }
  }

Tags: C# encoding Programming

Posted on Thu, 07 Nov 2019 12:42:44 -0500 by jj33