文件相关操作

  1. 判断文件是否已经存在

File.Exists(MyFileName) 返回为Bool值

  1. 判断文件夹是否已经存在

Directory.Exists(MyFolderName) 返回为Bool值

  1. 获得当前程序信息

公司名称:Application.CompanyName

区域信息:Application.CurrentCulture

语言标志:Application.CurrentInputLanguage

产品名称:Application.ProductName

产品版本:Application.ProductVersion

  1. 得到父目录信息

DirectoryInfo  myInfo=Directory.GetParent(MyFolderName)

  1. 显示根信息

Directory.GetDirectoryRoot(MyFolderName)

  1. 得到当前程序所在的文件夹

Directory.GetCurrentDirectory()

  1. 得到指定文件夹下的所有的文件

string[] MyFiles=Directory.GetFiles(MyFolderName)

  1. 得到指定文件夹下的所有子文件夹

string[] MyFolders=Directory.GetDirectories(MyFolderName)

  1. 删除文件夹

DirectoryInfo MyDir=new DirectoryInfo(MyFolderName);

if(MessageBox.Show(‘是否删除文件夹’+MyFolderName,”信息提示”,MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)

{

  MyDir.Delete();

}

  1. 创建文件夹

Directory.CreateDirectory(MyFolderName)

  1. 以流的形式读取文件

StreamReader streamRead=null;

try{

 streamRead=new StreamReader(MyFileName,System.Text.Encoding.Default)

}

catch(Exception ex)

{

  MessageBox.Show(“读取文件发生错误”);

}

finally

{

  if(streamRead!=null)

  {

 readStream.Close();

}

}

  1. 以流的形式写入文件

  StreamWriter streamWriter=null;

  try{

   streamWriter=new StreamWriter(MyFileName,false,System.Text.Encoding.Default);

   streamWriter.Write(Context); //Context为需要写入的内容

}

实例

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

 

 

namespace _1FileOpen

{

    class FileHelper

    {

        //类说明:该类提供文件相关操作

        /// <summary>

        /// 判断文件是否存在

        /// </summary>

        /// <param name="filePath">需要判断的文件名</param>

        /// <returns>返回值</returns>

        public static bool ExistFile(string filePath)

        {

            if (File.Exists(filePath))

            {

                return true;

            }

            else

            {

                return false;

            }

          

        }

 

        /// <summary>

        /// 判断文件夹是否存在

        /// </summary>

        /// <param name="folderPath">需要判断的文件夹名</param>

        /// <returns>返回值</returns>

        public static bool ExistDirectory(string folderPath)

        {

            if (Directory.Exists(folderPath))

            {

                return true;

            }

            else

            {

                return false;

            }

        }

 

        /// <summary>

        /// 得到父目录

        /// </summary>

        /// <param name="folderPath">子目录路径</param>

        /// <returns>父目录信息</returns>

        public static DirectoryInfo GetParentDirInfo(string folderPath)

        {

            return Directory.GetParent(folderPath);

        }

 

        /// <summary>

        /// 得到指定目录下的所有文件

        /// </summary>

        /// <param name="folderPath">指定目录路径</param>

        /// <returns>文件数组</returns>

        public static string[] GetFiles(string folderPath)

        {

            string[] files = Directory.GetFiles(folderPath);

            return files;

        }

 

        /// <summary>

        /// 得到指定目录下的所有子文件夹

        /// </summary>

        /// <param name="folderPath">指定目录路径</param>

        /// <returns>文件夹数组</returns>

        public static string[] GetDirectories(string folderPath)

        {

            string[] directories=Directory.GetDirectories(folderPath);

            return directories;

        }

 

        /// <summary>

        /// 删除文件

        /// </summary>

        /// <param name="folderPath">文件路径</param>

        /// <param name="isTrue">是否删除文件</param>

        public static void DeleteFile(string folderPath,bool isTrue)

        {

            DirectoryInfo files = new DirectoryInfo(folderPath);

            if (isTrue == true)

            {

                files.Delete();

            }

        }

 

        /// <summary>

        /// 移动文件

        /// </summary>

        /// <param name="sourcePath">原文件</param>

        /// <param name="targetPath">目标文件</param>

        public static void MoveFile(string sourcePath,string targetPath)

        {

            if (ExistFile(sourcePath) && !ExistFile(targetPath))

            {

                File.Move(sourcePath, targetPath);

            }

        }

 

        /// <summary>

        /// 创建文件夹

        /// </summary>

        /// <param name="filePath">需要创建的文件夹名称</param>

        public static void CreateDirectory(string filePath)

        {

            Directory.CreateDirectory(filePath);

        }

 

        /// <summary>

        /// 读取文件内容

        /// </summary>

        /// <param name="filePath">文件路径</param>

        /// <returns>返回读取的内容</returns>

        public static string ReadFile(string filePath)

        {

            StreamReader streamRead = null;

            streamRead=new StreamReader(filePath,Encoding.Default);

            string context = streamRead.ReadToEnd();

            streamRead.Close();

            return context;

        }

 

        /// <summary>

        /// 写入文件内容

        /// </summary>

        /// <param name="filePath">文件路径</param>

        /// <param name="context"></param>

        public static void WriteFile(string filePath,string context)

        {

            if (filePath != null)

            {

                StreamWriter streamWriter = new StreamWriter(filePath, false, Encoding.Default);

                streamWriter.Write(context);

                streamWriter.Close();

            }

        }

    }

}