(精华)2020年9月15日 ASP.NET Core 文件系统详解
【摘要】
1.文件系统遍历文件目录
public static class Sample01
{
public static void Run()
{
var provider =...
1.文件系统遍历文件目录
public static class Sample01
{
public static void Run()
{
var provider = new ServiceCollection()
.AddSingleton<IFileProvider>(new PhysicalFileProvider(@"e:\测试文件夹"))
.AddSingleton<FileManager>()
.BuildServiceProvider();
var fileManager = provider.GetService<FileManager>();
fileManager.Dir();
}
public class FileManager
{
private readonly IFileProvider _fileProvider;
public FileManager(IFileProvider fileProvider)
{
_fileProvider = fileProvider;
}
public void Dir()
{
var indent = -1;
void Get(string subpath)
{
indent++;
foreach (var fileinfo in _fileProvider.GetDirectoryContents(subpath))
{
Console.WriteLine(new string('\t',indent) + fileinfo.Name);
if (fileinfo.IsDirectory)
{
Get($@"{subpath}\{fileinfo.Name}");
}
}
}
Get("");
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
2.程序内嵌文件读取文件信息
public static class Sample02
{
public static void Run()
{
var provider = new ServiceCollection()
.AddSingleton<IFileProvider>(new EmbeddedFileProvider(Assembly.GetExecutingAssembly()))
.AddSingleton<FileManager>()
.BuildServiceProvider();
var fileManager = provider.GetService<FileManager>();
var content = fileManager.ReadAsync("Text.txt").Result;
Console.WriteLine(content);
}
public class FileManager
{
private readonly IFileProvider _fileProvider;
public FileManager(IFileProvider fileProvider)
{
_fileProvider = fileProvider;
}
public async Task<string> ReadAsync(string path)
{
await using var stream = _fileProvider.GetFileInfo(path).CreateReadStream();
var buffer = new byte[stream.Length];
await stream.ReadAsync(buffer, 0, buffer.Length);
return Encoding.Default.GetString(buffer);
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
3.监控文件系统,文件发生改变实时通知程序
public static class Sample03
{
public static void Run()
{
var provider = new ServiceCollection()
.AddSingleton<IFileProvider>(new PhysicalFileProvider(@"e:\其它"))
.AddSingleton<FileManager>()
.BuildServiceProvider();
var fileManager = provider.GetService<FileManager>();
fileManager.WatchAsync("Text.txt").Wait();
Console.Read();
}
public class FileManager
{
private readonly IFileProvider _fileProvider;
public FileManager(IFileProvider fileProvider)
{
_fileProvider = fileProvider;
}
public async Task WatchAsync(string path)
{
Console.WriteLine(await ReadAsync(path));
ChangeToken.OnChange(() => _fileProvider.Watch(path), async () =>
{
Console.Clear();
Console.WriteLine(await ReadAsync(path));
});
}
public async Task<string> ReadAsync(string path)
{
await using var stream = _fileProvider.GetFileInfo(path).CreateReadStream();
var buffer = new byte[stream.Length];
await stream.ReadAsync(buffer, 0, buffer.Length);
return Encoding.Default.GetString(buffer);
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
文章来源: codeboy.blog.csdn.net,作者:愚公搬代码,版权归原作者所有,如需转载,请联系作者。
原文链接:codeboy.blog.csdn.net/article/details/108412721
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)