Mrcdh技术博客 Mrcdh技术博客
首页
  • Html5
  • Javascript
  • Nodejs
  • electron
  • Android
  • 微信公众号
  • 框架
  • 其他
  • Mysql
  • PHP
  • Python
  • java
  • Gulp
  • 其它
  • 网站
  • 资源
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Mrcdh

全栈开发小学生
首页
  • Html5
  • Javascript
  • Nodejs
  • electron
  • Android
  • 微信公众号
  • 框架
  • 其他
  • Mysql
  • PHP
  • Python
  • java
  • Gulp
  • 其它
  • 网站
  • 资源
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Mysql

    • mysql基本使用
    • 总结下mysql8在windows环境下的安装和配置
    • Mysql常用函数
    • Mysql常用语句
  • PHP

  • Python

  • java

  • Go

  • 数据库

  • C#

  • ElasticSearch

  • Git

  • Gulp

  • Microsoft

  • Linux

  • 其它

  • 技术
  • C#
  • winform
mrcdh
2024-07-11
目录

常见路径文件操作

# 路径

// 程序运行路径
System.Wmdows.Forms.Application.StartupPath + "\\";

// 桌面
Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
// 收藏夹
Environment.GetFolderPath(Environment.SpecialFolder.Favorites);
// 我的文档
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
// 最新使用的文档
Environment.GetFolderPath(Environment.SpecialFolder.Recent);
1
2
3
4
5
6
7
8
9
10
11

# 文件操作

string aFile = "D:\\test.txt";
string toFile = "D:\\test2.txt";
// 文件是否存在
File.Exists(aFile);
// 复制文件
File.Copy(aFile, toFile);
// 移动文件,目标目录必须存在
File.Move(aFile, toFile);
// 删除文件
File.Delete(aFile);
// 获取文件目录部分
Path.GetDirectoryName(toFile);
// 重命名,未提供重命名方法,建议使用vb的
1
2
3
4
5
6
7
8
9
10
11
12
13

文件属性 FileInfo info = new FileInfo(aFile);

方法 说明
info.Exists 是否存在
info.Name 文件名
info.Length 文件大小
info.CreationTime 创建时间
info.LastAccessTime 最后访问时间
info.LastWriteTime 最后修改时间
info.Attributes 文件属性
info.Extension 文件扩展名
info.IsReadOnly 是否只读
info.Directory.Name 文件所在目录

# 文件夹操作

// 未提供重命名和复制方法,建议使用vb的
// 文件夹是否存在
Directory.Exists(dirName);
// 查看文件夹属性
DirectoryInfo info = new DirectoryInfo(dirName);
// info...

// 移动文件夹
Directory.Move(dirName, toDir);
// 设置当前操作文件夹
Directory.SetCurrentDirectory(@"C:\");
// 创建文件夹
Directory.CreateDirectorγ(DateTime.Now.ToString("yyyyMMddHHmmss"));
// 删除文件夹
Directory.Delete(dirName, true);

// 遍历文件夹中的文件
foreach (FileInfo file in info.GetFiles()){
    Console.WriteLine(file.FullName);
}
// 遍历文件夹中的子文件夹
foreach(DirectoryInfo dInfo in info.GetDirectories()){
    Console.WriteLine(dInfo.FullName);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#winform
上次更新: 2024/07/11, 13:56:03
最近更新
01
uniapp常见问题
03-19
02
Vue3项目搭建
12-25
03
使用pnpm的monorepo组织项目
11-01
更多文章>
Theme by Vdoing | Copyright © 2020-2025 Mrcdh | 苏ICP备2020058908号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×