欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# 文件操作函数 创建文件 判断存在

程序员文章站 2022-06-05 14:14:01
文件创建: 复制代码 代码如下:file.create(application.startuppath + "\\alarmset.txt");//创建该文件 sys...

文件创建:

复制代码 代码如下:
file.create(application.startuppath + "\\alarmset.txt");//创建该文件
system.io.path.getfilename(filepath)       //返回带扩展名的文件名
system.io.path.getfilenamewithoutextension(filepath)     //返回不带扩展名的文件名
system.io.path.getdirectoryname(filepath)     //返回文件所在目录

文件存在:

复制代码 代码如下:
file.exists(application.startuppath + "\\alarmset.txt") 
system.io.directoryinfo info = new system.io.directoryinfo(application.startuppath + "\\alarmset.txt"); 
messagebox.show(info.exists.tostring());

是不是很简单,但有时候我们可能对getfilenamewithoutextension或getdirectoryname这样的函数,感觉比较陌生,以前还总傻傻的自己写函数实现,原来就一行代码搞定啊~