[WPF]程序运行路径,目录,文件名,扩展等的获取,目录文件不存在自动创建

来源:赵克立博客 分类: WPF 标签:--发布时间:2017-08-21 11:50:30最后更新:2017-08-21 12:03:19浏览:2727
版权声明:
本文为博主原创文章,转载请声明原文链接...谢谢。o_0。
更新时间:
2017-08-21 12:03:19
温馨提示:
学无止境,技术类文章有它的时效性,请留意文章更新时间,如发现内容有误请留言指出,防止别人"踩坑",我会及时更新文章

每一门语言必不可少的跟路径,目录,文件打交道

取程序运行目录

string appprunath = System.AppDomain.CurrentDomain.BaseDirectory;

结果为 如图所示路径

image.png

分割路径中的一些信息

string filepath = "c:/logs/12.log";
var dirpath = System.IO.Path.GetDirectoryName(filepath);//resutl is   c:\\logs
var filename = System.IO.Path.GetFileNameWithoutExtension(filepath);//result is   12
var filep = System.IO.Path.GetFileName(filepath);//result is   12.log

判断文件或路径是否存在,不存在创建

//LOG目录不存在,则创建
var filepath = "c:/logs/201708.log";
var pat=Path.GetDirectoryName(filepath);
if (Directory.Exists(pat) == false)
{
    Directory.CreateDirectory(pat);
}
//日志文件不存在,则创建
if (File.Exists(filepath) == false)
{
    File.Create(filepath).Close();
}



微信号:kelicom QQ群:215861553 紧急求助须知
Win32/PHP/JS/Android/Python