C# 文件操作

获取启动项路径   

1.Environment.CurrentDirectory    System.IO.Path.Combine(Environment.CurrentDirectory, @"Data\Data.db")

2.System.Windows.Forms.Application.StartupPath

3. System.AppDomain.CurrentDomain.BaseDirectory     System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, @"config.xml") // 服务用这个

 

获取文件列表

DirectoryInfo dirInfo = new DirectoryInfo(System.IO.Path.Combine(Environment.CurrentDirectory, @"Models"));
foreach (FileInfo item in dirInfo.GetFiles("*.md"))
{
  ControlModelFile.Add(item.Name);
}

 删除文件

FileInfo fileInfo = new FileInfo(path);
fileInfo.Delete();

 确认文件是否存在

File.Exists(FileInfo.FullName)

 检查文件夹是否存在,不存在新建

string sPath = $"D:/Report/{deviceID}";
if (!Directory.Exists(sPath))
{
    Directory.CreateDirectory(sPath);
}

 选择文件

OpenFileDialog openPath = new OpenFileDialog();
openPath.InitialDirectory = System.AppDomain.CurrentDomain.BaseDirectory;
if (openPath.ShowDialog() != true)
{
    return;
}
string FilePath = openPath.FileName;

 

posted @ 2021-06-29 08:28  妖言惑众'  阅读(56)  评论(0)    收藏  举报