C# 对文件、文件夹的操作

本文详细介绍了C#中对文件(包括判断文件存在、打开、复制、追加内容等)和文件夹(如路径获取、创建、移动、遍历等)的基本操作,以及如何检测exe应用程序的运行状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在使用后面的代码前,需要:

using System;
using System.IO;

C# 对文件的操作

  1. 判断文件是否存在
string filePath = "E:\\new folder\\test\\myfile.xls";
if (File.Exists(filePath))
{
	// 如果文件存在
}
else
{
	// 如果文件不存在
}
  1. 打开指定的文件
string filePath = "E:\\new folder\\test\\myfile.xls";
if (File.Exists(filePath))
{
	// 如果文件存在
	System.Diagnostics.Process.Start(filePath);
}
else
{
	// 如果文件不存在
	Console.WriteLine(savePath + " does not exist!");
}
  1. 打开文件选择框
string loadFilePath = "";
OpenFileDialog Openfile = new OpenFileDialog();
Openfile.Filter = "Excel file|*.xls;*.xlsx";
// openFile.Filter="(*.jpg,*.png,*.jpeg,*.bmp,*.gif)|*.jgp;*.png;*.jpeg;*.bmp;*.gif|All files(*.*)|*.*";
// openFile.Filter = "图片|*.gif;*.jpg;*.jpeg;*.bmp;*.jfif;*.png;|文档|*.doc;|Excel|*.xls;*.xlsx";
if (Openfile.ShowDialog() == DialogResult.OK)
{
	loadFilePath = Openfile.FileName;
}
else
{
	Console.WriteLine("False, please load the file again!");
}
  1. 复制文件
string oldFileName = "E:\\new folder\\test\\myfile.xls";
string newFileName = "E:\\new folder\\test\\myfile_copy.xls";
File.Copy(oldFileName, newFileName, ture); // 将现有文件复制到新文件, 允许覆盖同名的文件。
// File.Copy(oldFileName, newFileName, false); // 将现有文件复制到新文件, 不允许覆盖同名的文件。
  1. 往 txt 文件中追加内容
string Year = DateTime.Now.Year.ToString(); // 获取当前年份
string Month = DateTime.Now.Month.ToString().PadLeft(2, '0'); // 获取当前月份
string Day = DateTime.Now.Day.ToString().PadLeft(2, '0'); // 获取当前是几号
string Hour = DateTime.Now.Hour.ToString().PadLeft(2, '0');
string Minute = DateTime.Now.Minute.ToString().PadLeft(2, '0');
string Second = DateTime.Now.Second.ToString().PadLeft(2, '0');

string filePath = "E:\\new folder\\test\\myfile.txt";
FileStream fs = new FileStream(filePath, FileMode.Append);
StreamWriter wr = null;
wr = new StreamWriter(fs);
wr.WriteLine("Date: " + Year + "." + Month + "." + Day + "\n");
wr.WriteLine("Time: " + Hour + ":" + Minute + ":" + Second + "\n");
wr.Close();
  1. 判断某个exe应用程序是否在运行
public static bool IsRuning(string exeName)
{
var res = false;
if (Process.GetProcessesByName(exeName).ToList().Count > 0)
{
	res = true;
}
return res;
}

C# 对文件夹的操作

  1. 获取当前文件夹的路径
string currentWorkPath = System.IO.Directory.GetCurrentDirectory();
// currentWorkPath = "E:\\new folder\\test";
  1. 创建文件夹
string folderPath = "E:\\new folder\\test";
Directory.CreateDirectory(folderPath);
  1. 判断文件夹是否存在
string folderPath = "E:\\new folder\\test";
if (Directory.Exists(folderPath))
{
	// 如果文件夹存在
}
else
{
	// 如果文件夹不存在
}
  1. 移动文件夹
string sourceDirectory = @"C:\source";
string destinationDirectory = @"C:\destination";
try
{
	Directory.Move(sourceDirectory, destinationDirectory);
}
catch (Exception e)
{
	Console.WriteLine(e.Message);
}
  1. 读取一个文件夹下所有的 .xls 文件
string folderPath = "E:\\new folder\\test";
string[] files = Directory.GetFiles(folderPath + @"\", "*.xls");
  1. 打开文件夹选择框
string CurrentPath = "";
System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
dialog.Description = "Please select the folder";
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
	CurrentPath = dialog.SelectedPath;
	Console.WriteLine("成功加载文件夹: "+ CurrentPath);
}
else
{
	Console.WriteLine("False, please load the folder again!");
}
  1. 打开指定的文件夹
string savePath = "D:\\Program files";
if (Directory.Exists(savePath))
{
	System.Diagnostics.Process.Start(savePath);
}
else
{
	Console.WriteLine(savePath + " does not exist!");
}
  1. 获取指定文件夹下所有文件夹名称
public static string[] GetsubFoldersPath(string path)
{
	if (Directory.Exists(path))
	{
		//文件路径
		string[] dir = Directory.GetDirectories(path);
		//文件名
		string[] names = new string[dir.Length];
		for (int i = 0; i < dir.Length; i++)
		{
			//赋值文件命名
			names[i] = Path.GetFileName(dir[i]);
		}
		return names;
	}
	else
	{
		Console.WriteLine(path + " does not exist!");
		return null;
	}
}

参考

  1. C#.NET读取一个文件夹下所有excel文件的代码
  2. 微软 .Net System.IO File.Copy 方法
  3. 简单到复杂:C#拷贝文件的3种方法
  4. C# OpenFileDialog打开文件对话框(详解)
  5. OpenFileDialog对话框过滤文件类型(Filter)介绍
  6. C# 获取当前程序运行路径
  7. C#打开文件、文件夹对话框
  8. System.Diagnostics.Process.Start(); 主要功能
  9. C#获取指定文件夹下所有文件夹名称
  10. System.IO File类
  11. c# 判断某个exe应用程序运行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值