Csharp-04
DLL
DLL(Dynamic Link Library)是动态链接库的缩写。它是一种包含可供程序调用的函数和数据的文件。DLL 文件可以包含多个函数,这些函数可以被多个程序调用。DLL 文件的扩展名是 .dll。
创建 DLL
新建一个类库项目。
在类库项目中添加类。
在类库项目中编写类的代码。
在类库项目中编译代码。
在类库项目的输出目录中找到编译后的 DLL 文件。
使用 DLL
在项目中添加引用。
在项目中使用引用的类。
比如,我需要引用一个 example.dll 文件,我可以在项目中添加引用,然后使用引用的类。
1 | using Example; |
文件操作
文件读取
1 | using System; |
文件写入
1 | using System; |
文件追加
1 | using System; |
文件删除
1 | using System; |
e.g
编写一个程序,读取一个文件,然后将文件内容倒序写入另一个文件,比如 example.txt 内容为 Hello, World!,则 example-reverse.txt 内容为 !dlroW ,olleH。
创建 example.txt 文件。
给 example.txt 写入内容为 `Hello, World!
读取 example.txt 文件。
将读取的内容打印到控制台。
将读取的内容倒序写入 example-reverse.txt 文件。
查看 example-reverse.txt 文件。
删除 example.txt 和 example-reverse.txt 文件。
1 | using System; |
其他文件操作
文件操作类
File。
File.Copy复制文件。File.Move移动文件。File.Exists判断文件是否存在。File.GetCreationTime获取文件创建时间。File.GetLastAccessTime获取文件最后访问时间。File.GetLastWriteTime获取文件最后写入时间。
获取文件夹内容可以使用
Directory类。
Directory.GetFiles获取文件夹中的文件。Directory.GetDirectories获取文件夹中的文件夹。