文件(夹)的相关操作

新建文件夹

If(!Directory.Exist(路径))
Directory.CreateDirectory(路径);

删除文件夹

Directory.Delete(路径,true);//true:删除含有的子目录和文件,如果不加true的话,文件夹需要为空

移动、重命名文件夹

Directory.Move(“源”,“目标”)//注意,只能是同个驱动器下面移动,

遍历文件夹

//返回子目录:
Directory.GetDirectories(,) //返回sting[]
Dinfo.GetDirectories() //返回Dinfo[] //返回包含文件
Directory.GetFiles(,) //返回sting[]
Dinfo.GetFiles() //返回Dinfo[]
//返回包含子目录+文件
Dinfo.GetFileSystemInfos()

新建文件、判断文件是否存在


If (!File.Exist())
File.Creat(文件名); //--->如果只想执行一个操作 可以用File类//(也可用FileInfo)

移动或复制文件

String sourcePath = ”...";
String targetPath = "....."; // 移动时,新路径中可以重命名文件
bool isrewrite = true; // 复制时,true-覆盖已存在的同名文件,false反之 File.Copy(sourcePath, targetPath, isrewrite);
File.Move(sourcePath, targetPath);

读文件

FileInfo file = new FileInfo(路径);   //--->如果只想执行一个操作 可以用File类
StreamReader Reader = new StreamReader(file.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite));
line = Reader.ReadLine();
//读取位置返回到第一行
Reader.BaseStream.Position = 0;
//读取防乱码
StreamReader Reader = new StreamReader(filepath,unicodeEncoding.GetEncoding(“GB_2312”));

写文件

FileInfo file = new FileInfo(路径);
StreamWriter Writer = new StreamWriter(file.Open(FileMode.Create, FileAccess.Write, FileShare.ReadWrite));
Writer.WriteLine("一行内容");
Writer.Flush();//清空缓冲区
Writer.Close();

表格Gridview

设置表格[数组形式]

DataTable dt = new DataTable();
dt.Columns.Add(列1表头.ToString(), typeof(int));
dt.Columns.Add(列2表头.ToString(), typeof(int));
dt.Rows.Add(string[] dr);
DataRow dr = dt.NewRow();
for (int j = 0; j < ctrlOut0.ctrl.GetLength(1); j++)
{
dr[j] = ctrlOut0.ctrl[i, j];
}
dt.Rows.Add(dr);

Datagridview1.DataSource=dt;

表格禁用自动排序,并设置列选择

for (int i = 0; i < dtv.Columns.Count; i++)
{
dtv.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
}
dtv.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;

Chart控件

画图

Chart.Series[0].chartType=seriesChartType.Point;
Chart.Series[0].Points.DataBindXY(x[],y[]);

或:

For(int i=0;i<xxx;i++)
{
Chart.Series[0].Points.AddXY(x[i],y[i]));
}

GDI

//画图
Graphics g = Graphics.FromImage(bit);
Rectangle a = new Rectangle(0,0,W,W); //指定画图的大小
Rectangle b = new Rectangle(p * 40, 0, 80, 80); //指定图源的位置与大小
g.DrawImage(Source, a, b,GraphicsUnit.Pixel);

新建进程

System.Diagnostics.Process.Start("explorer.exe", 路径);

正则 以空格分割字符串

line = line.Trim();
line = new Regex("[\\s]+").Replace(line, " ");
string[] tmp = line.Split(new char[] { ' ' });

控件拖拽事件

//DragEnter
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Link;
else e.Effect = DragDropEffects.None; //DragDrop
string fileName = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
txt.Text = fileName;

读取文件对话框

if (dlgOpen.ShowDialog() != DialogResult.OK) return;
_fileName = dlgOpen.FileName; //需要定义

C#常用代码(更新中)的更多相关文章

  1. Android Studio ---------------常用快捷键(更新中。。。。。。)

    ##常用快捷键: Ctrl+X(或Y) 删除行 Ctrl+D 复制行 Ctrl+Alt+L 格式化代码 Ctrl + Alt + V 提取变量 Shift+F6 重命名 Ctrl+F12显示当前文件的 ...

  2. java常用注解(更新中)

    注解根据来源可分为: 系统注解(自带的,取决于JDK版本).自定义注解及第三方注解 系统注解根据用途又可分为: java内置注解和元注解 根据运行机制(保留到什么时候)可分为: 源码注解.编译注解和运 ...

  3. Mysql 数据库之常用命令[更新中...]

    连接命令:mysql -h[主机地址] -u[用户名] -p[用户密码] 创建数据库:create database [库名] 例如:create database jtest; 显示所有数据库: s ...

  4. [C#常用代码]类库中读取解决方案web.Config字符串

    对于类库里读取解决方案web.config文件里字符串的方法一.读取键值对的方法:1.添加引用using System.Configuration;2.web.Config配置节<appSett ...

  5. 【前端】Util.js-ES6实现的常用100多个javaScript简短函数封装合集(持续更新中)

    Util.js (持续更新中...) 项目地址: https://github.com/dragonir/Util.js 项目描述 Util.js 是对常用函数的封装,方便在实际项目中使用,主要内容包 ...

  6. IntelliJ Idea 常用快捷键总结-0 #<间断性更新中...>,部分有示例

    IntelliJ Idea 常用快捷键总结-0 <间断性更新中...>,部分有示例 自动补齐代码 常用的有for循环体,fori可以输出循环语句: eg: public void test ...

  7. redis整理:常用命令,雪崩击穿穿透原因及方案,分布式锁实现思路,分布式锁redission(更新中)

    redis个人整理笔记 reids常见数据结构 基本类型 String: 普通key-value Hash: 类似hashMap List: 双向链表 Set: 不可重复 SortedSet: 不可重 ...

  8. git常用命令(持续更新中)

    git常用命令(持续更新中) 本地仓库操作git int                                 初始化本地仓库git add .                       ...

  9. 知道创宇爬虫题--代码持续更新中 - littlethunder的专栏 - 博客频道 - CSDN.NET

    知道创宇爬虫题--代码持续更新中 - littlethunder的专栏 - 博客频道 - CSDN.NET undefined 公司介绍 - 数人科技 undefined

随机推荐

  1. [python,2018-03-06] python中的继承顺序

    python 支持多继承,但对与经典类和新式类来说,多继承查找的顺序是不一样的.  经典类: 新式类   class P1:      def foo(self):                   ...

  2. java面试题收集

    http://www.cnblogs.com/yhason/archive/2012/06/07/2540743.html 2,java常见面试题 http://www.cnblogs.com/yha ...

  3. 201. Spring Boot JNDI:Spring Boot中怎么玩JNDI

      [视频&交流平台] àSpringBoot视频:http://t.cn/R3QepWG à SpringCloud视频:http://t.cn/R3QeRZc à Spring Boot源 ...

  4. centos7使用rpm方式安装mysql

    https://blog.csdn.net/smiles13/article/details/81460617    (部分参考) 先查看是否安装mariadb rpm -qa | grep mari ...

  5. JavaScript学习-4——DOM对象、事件

    本章目录 --------window对象 --------document对象 --------事件 一.window对象 函数调用: 自己封装的函数只写:函数名(): 数学函数Math 例:绝对值 ...

  6. 一篇面经(BAT面试)(转)

    0. 写在之前 首先呢我的面试经历和一些面霸和收割机的大神相比绝不算丰富,但我这三个月应该能代表很大一部分人的心路历程:从无忧无虑也无知的状态,然后遭遇挫败,跌入低谷,连续数天的黑暗,慢慢调整,逼着自 ...

  7. TypeError: Fetch argument None has invalid type <type 'NoneType'>

    (fetch, type(fetch)))TypeError: Fetch argument None has invalid type <type 'NoneType'> 我的解决方案是 ...

  8. yii2 basic版本的一些配置

    1.nginx配置 重写规则 修改访问模式为 http://wh.store/admin/index 文件位置: /home/wwwroot/default/yii2-app-basic/config ...

  9. VueJs学习笔记

      在cmd下,进入目录之后 cd 到项目目录下 1 安装node cnpm install   2 启动或者调试 cnpm start (或是npm run dev) 3 上线: npm run b ...

  10. python的arp扫描

    python的arp扫描 from optparse import *from scapy.all import *parser = OptionParser()parser.add_option(& ...