C#FileHelper
/**
*┌──────────────────────────────────────────────────────────────┐
*│ 描 述:文件IO的工具类
*│ 作 者:执笔小白
*│ 版 本:4.0
*│ 创建时间:2022-6-13 15:40:56
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│ 命名空间: ZhibiXiaobai.Uril.IOHelper
*│ 类 名:FileIOHelper
*└──────────────────────────────────────────────────────────────┘
*/
using System.IO;
using System.Windows.Forms;
namespace ZhibiXiaobai.Uril.IOHelper
{
public class FileIOHelper
{
/// <summary>
/// 判断文件存不存在
/// <param name="FilePath">文件路径</param>
/// <param name="IsCreate">不存在时是否创建文件</param>
/// <returns>返回判断的结果</returns>
public static bool ISExists_File(string FilePath, bool IsCreate = false)
{
if (File.Exists(FilePath))
{
return true;
}
else
{
if (IsCreate) // 需不需要创建文件
{
//检验目录不存在就创建
//if (!System.IO.Directory.Exists(FilePath))
//{
// System.IO.Directory.CreateDirectory(FilePath);
//}
FilePath = FilePath.Replace(@"\\", @"/"); // 格式化目录 '\\'变为'/'
string[] ss = FilePath.Split(new char[] { '/' });
string filePathP = ss[0];
for (int i = 1; i < ss.Length - 1; i++)
{
filePathP += "/" + ss[i];
}
if (!System.IO.Directory.Exists(filePathP))
{
System.IO.Directory.CreateDirectory(filePathP);
}
File.Create(FilePath);
}
return false;
}
}
#region 通过路径取文件方法
/// <summary>
/// 通过文件对话框取文件路径
/// 使用 var filename = OpenfileDlg();
/// </summary>
/// <param name="Defaultpath">默认打开路径</param>
/// <param name="Fileter">文件类型</param>
/// <param name="Multiselect">是否支持选择多个文件</param>
/// <param name="DlgTitle">Dlg窗体的标题</param>
/// <param name="LimitSize">是否限制大小(单位为KB,0为不限制)</param>
/// <returns></returns>
public static string OpenfileDlg(string Defaultpath = null, string Fileter = ".jpeg|*.jpeg|.jpg|*.jpg|.png|*.png|.gif|*.gif|.bmp|*.bmp", bool Multiselect = false,
string DlgTitle = "请选择要打开的文件",long LimitSize=0)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = DlgTitle; // 标题
ofd.Multiselect = Multiselect; // 多选
ofd.InitialDirectory = Defaultpath; // 初始目录
ofd.Filter = Fileter; // 设定文件类型
ofd.ShowDialog(); // .GetHashCode();
string path = ofd.FileName; // 获得在打开文件对话框中选择的文件的路径
if (!string.IsNullOrEmpty(path) && LimitSize > 0 && ofd.OpenFile().Length > (LimitSize * 1024))
{
path = null;
}
return path;
}
#endregion
/// <summary>
/// 复制-不覆盖已有文件
/// </summary>
/// <param name="SourceFileUrl">源文件地址</param>
/// <param name="DistFileUrl">目标地址</param>
public static void CopyFile(string SourceFileUrl,string DistFileUrl)
{
File.Copy(SourceFileUrl, DistFileUrl);
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
作者:꧁执笔小白꧂