在使用后面的代码前,需要:
using System;
using System.IO;
C# 对文件的操作
- 判断文件是否存在
string filePath = "E:\\new folder\\test\\myfile.xls";
if (File.Exists(filePath))
{
// 如果文件存在
}
else
{
// 如果文件不存在
}
- 打开指定的文件
string filePath = "E:\\new folder\\test\\myfile.xls";
if (File.Exists(filePath))
{
// 如果文件存在
System.Diagnostics.Process.Start(filePath);
}
else
{
// 如果文件不存在
Console.WriteLine(savePath + " does not exist!");
}
- 打开文件选择框
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!");
}
- 复制文件
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); // 将现有文件复制到新文件, 不允许覆盖同名的文件。
- 往 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();
- 判断某个exe应用程序是否在运行
public static bool IsRuning(string exeName)
{
var res = false;
if (Process.GetProcessesByName(exeName).ToList().Count > 0)
{
res = true;
}
return res;
}
C# 对文件夹的操作
- 获取当前文件夹的路径
string currentWorkPath = System.IO.Directory.GetCurrentDirectory();
// currentWorkPath = "E:\\new folder\\test";
- 创建文件夹
string folderPath = "E:\\new folder\\test";
Directory.CreateDirectory(folderPath);
- 判断文件夹是否存在
string folderPath = "E:\\new folder\\test";
if (Directory.Exists(folderPath))
{
// 如果文件夹存在
}
else
{
// 如果文件夹不存在
}
- 移动文件夹
string sourceDirectory = @"C:\source";
string destinationDirectory = @"C:\destination";
try
{
Directory.Move(sourceDirectory, destinationDirectory);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
- 读取一个文件夹下所有的 .xls 文件
string folderPath = "E:\\new folder\\test";
string[] files = Directory.GetFiles(folderPath + @"\", "*.xls");
- 打开文件夹选择框
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!");
}
- 打开指定的文件夹
string savePath = "D:\\Program files";
if (Directory.Exists(savePath))
{
System.Diagnostics.Process.Start(savePath);
}
else
{
Console.WriteLine(savePath + " does not exist!");
}
- 获取指定文件夹下所有文件夹名称
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;
}
}