惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Vercel News
Vercel News
Y
Y Combinator Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The GitHub Blog
The GitHub Blog
N
Netflix TechBlog - Medium
MyScale Blog
MyScale Blog
F
Fortinet All Blogs
Microsoft Azure Blog
Microsoft Azure Blog
H
Help Net Security
C
Check Point Blog
博客园 - 聂微东
云风的 BLOG
云风的 BLOG
M
MIT News - Artificial intelligence
U
Unit 42
WordPress大学
WordPress大学
B
Blog
Last Week in AI
Last Week in AI
人人都是产品经理
人人都是产品经理
T
Tailwind CSS Blog
D
DataBreaches.Net
G
Google Developers Blog
T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
IT之家
IT之家

博客园 - 空明流光

c# 正则替换与代码动态替换 正则断言备忘 c# await 异步编程工具类 windows 上 使用 conda 以 指定环境 启动 自定义程序 WPF 数据绑定通过 ElementName 失效后改为 Reference 正常 c# 获取当前时间 与跨域 iframe 通信示例 svn 忽略选项还原 android 应用分身无限多开,开源免费应用 使用 chrome 调试 android webview 前端 dom script sql server 折腾时不小心去掉了 sysadmin 权限 wpf 中显示约束只创建一个窗口实例 sqlcipher/sqlite 加解密 Xamarin Android 动态修改菜单颜色 XABLD7000: Xamarin.Tools.Zip.ZipException: Renaming temporary file failed: Permission denied .net8 项目指定包还原位置和读取位置以便备份依赖项 wpf 访问内置资源 C# UTF-8 各种编码比较 vs2022 wpf 无法识别 xaml 中定义的控件名称 wpf 使用 cursor 命令 wpf datagrid 过滤数据两种方式?
C# 文件同步写入锁定
空明流光 · 2025-09-26 · via 博客园 - 空明流光
void Main()
{
    var path = @"D:\test.txt";
    
    using (FileStream fs = new FileStream(
                path,
                FileMode.OpenOrCreate, // 打开或创建文件
                FileAccess.Write,      // 请求写入权限
                FileShare.Read))       // 关键:允许其他进程读取,但不允许它们写入[1,6](@ref)
    {
        
        FileInfo fileInfo = new FileInfo(path);
        
        // 判断文件在最后一次读取后是否有被人写入过。如果有,不能保存;如果没有,直接保存。
        
        fileInfo.LastWriteTime.Dump();
        
        Thread.Sleep(100000);
        
//        // 将字符串内容转换为字节数组并写入文件
//        byte[] data = Encoding.UTF8.GetBytes(content);
//        fs.Write(data, 0, data.Length);
//        Console.WriteLine("内容已成功写入文件,文件处于锁定状态。");
//
//        // 此处可以模拟一个长时间的操作,在此期间文件锁保持有效
//        // Console.WriteLine("按任意键释放文件锁...");
//        // Console.ReadKey();
    }
}

// You can define other methods, fields, classes and namespaces here