使用C# 实现文件锁

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace FileTokenNotWork
{
    public partial class Form1 : Form
    {
        FileStream fs = null;
        int count = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.ShowDialog();
            filedirtectory.Text = ofd.FileName;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            count++;
            if (count > 1)
            {
                MessageBox.Show("Do not do it again.","Warning",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                return;
            }
            try
            {
                if (string.IsNullOrEmpty(filedirtectory.Text))
                {
                    MessageBox.Show("Not have the file", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    count--;
                    return;
                }
                fs = new FileStream(filedirtectory.Text, FileMode.Open, FileAccess.Read, FileShare.None);
            }
            catch (Exception)
            {
                fs.Close();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            count--;
            fs.Close();
        }
    }
}
namespace FileTokenNotWork
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.btnUpdate = new System.Windows.Forms.Button();
            this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
            this.filedirtectory = new System.Windows.Forms.TextBox();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.groupBox1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.SuspendLayout();
            //
            // btnUpdate
            //
            this.btnUpdate.Location = new System.Drawing.Point(175, 20);
            this.btnUpdate.Name = "btnUpdate";
            this.btnUpdate.Size = new System.Drawing.Size(75, 23);
            this.btnUpdate.TabIndex = 0;
            this.btnUpdate.Text = "submit";
            this.btnUpdate.UseVisualStyleBackColor = true;
            this.btnUpdate.Click += new System.EventHandler(this.btnUpdate_Click);
            //
            // openFileDialog
            //
            this.openFileDialog.FileName = "openFileDialog";
            //
            // filedirtectory
            //
            this.filedirtectory.Location = new System.Drawing.Point(18, 22);
            this.filedirtectory.Name = "filedirtectory";
            this.filedirtectory.Size = new System.Drawing.Size(134, 21);
            this.filedirtectory.TabIndex = 1;
            //
            // groupBox1
            //
            this.groupBox1.Controls.Add(this.filedirtectory);
            this.groupBox1.Controls.Add(this.btnUpdate);
            this.groupBox1.Location = new System.Drawing.Point(12, 12);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(268, 65);
            this.groupBox1.TabIndex = 2;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Find file directory";
            //
            // groupBox2
            //
            this.groupBox2.Controls.Add(this.button2);
            this.groupBox2.Controls.Add(this.button1);
            this.groupBox2.Location = new System.Drawing.Point(12, 97);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(268, 98);
            this.groupBox2.TabIndex = 3;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "lock file";
            //
            // button1
            //
            this.button1.Location = new System.Drawing.Point(28, 37);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "lock";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // button2
            //
            this.button2.Location = new System.Drawing.Point(144, 37);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 1;
            this.button2.Text = "release";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(290, 204);
            this.Controls.Add(this.groupBox2);
            this.Controls.Add(this.groupBox1);
            this.Name = "Form1";
            this.Text = "LockFileTool";
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.groupBox2.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Button btnUpdate;
        private System.Windows.Forms.OpenFileDialog openFileDialog;
        private System.Windows.Forms.TextBox filedirtectory;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.GroupBox groupBox2;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button1;
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace FileTokenNotWork
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

很简单的代码,不解释了

使用C# 实现文件锁

时间: 2024-08-25 00:12:52

使用C# 实现文件锁的相关文章

Mysql的锁机制与PHP文件锁处理高并发简单思路

以购买商品举例: ① 从数据库获取库存的数量. ② 检查一下库存的数量是否充足. ③ 库存的数量减去买家购买的数量(以每个用户购买一个为例). ④ 最后完成购买. 仅仅这几行逻辑代码在并发的情况下会出现问题,自己可以想象一下. 这里暂时就不测试了,下面会针对并发的处理给出测试结果. 创建表: CREATE TABLE `warehouse` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id', `stock` int(11) NOT NULL

文件锁的玩法

群里大家一起交流:410028331 1.普通两个文件同一时候往一个文件里写入内容效果 index-1.php <?php $file = 'temp.txt'; $fp = fopen($file,'a'); for($i = 0;$i <10;$i++) { fwrite($fp, "11111111<br />"); sleep(1); } fclose($fp); ? > index-2.php <?php $file = 'temp.txt

文件锁-FileLock

最近在看flume部分功能的源码,关于FileLock的使用,其实在很多开源框架都有涉及,我所看过的有lucene,zookeeper,hadoop,es等开源框架都有用到,下面简单的介绍下FileLock. 1,FileLock是独占锁,控制不同程序(JVM)对同一文件的并发访问.         2,可以对写文件(w)加锁,而且必须是可写文件,不然回报:java.nio.channels.NonWritableChannelException异常,这样可以保证只有同一个进程才能拿到锁对文件访

fcntl函数加文件锁

对文件加锁是原子性的,可以用于进程间文件操作的同步.在linux下,有三个函数可以对文件进程加锁,分别是fcntl.flock.lockf.这里只说fcntl,它的用法也是最复杂的. fcntl是file control的缩写.在linux下大部分设备都是文件,所以fcntl的功能也比较多,包括: Duplicating a file descriptor(复制文件描述符) File descriptor flags(操作close-on-exec标志) File status flags(操作

Android系统开发(7)——标准I/O与文件锁

一.常用函数 fopen: FILE *fopen(const char *filename, const char *mode); fread: size_t  fread(void *ptz, size_t size, size_t nitems, FILE *stream); fwrite: size_t fwrite(const void *ptz, size_t size, size_t nitems, FILE *stream); fclose: int fclose(FILE *s

Linux文件锁学习-flock, lockf, fcntl

参考  linux中fcntl().lockf.flock的区别 这三个函数的作用都是给文件加锁,那它们有什么区别呢? 首先flock和fcntl是系统调用,而lockf是库函数.lockf实际上是fcntl的封装,所以lockf和fcntl的底层实现是一样的,对文件加锁的效果也是一样的.后面分析不同点时大多数情况是将fcntl和lockf放在一起的. 下面首先看每个函数的使用,从使用的方式和效果来看各个函数的区别. 1. flock l 函数原型 #include<sys/file.h> i

[转载] 文件锁(Filelock)与锁定映射文件部分内容

转载自http://jiangzhengjun.iteye.com/blog/517677 文件锁 JDK 1.4引入了文件加锁机制,允许我们同步访问一个共享文件,不过,竞争同一文件的两个线程有可能在不同的java虚拟机上,或者一个是java线程,另一个是操作系统中其他的某个线程,但文件锁对其他线程或其他操作系统进程都是可见的,因为java的文件加锁直接映射到了本地操作系统的加锁机制.注,这里讲的锁是指锁定其他应用程序,而不是锁定同一虚拟机里访问的同一文件的其他线程 .如果在同一虚拟机两次锁定同

PHP.37-扩展-锁机制解决并发-MySQL锁、PHP文件锁

锁机制适用于高并发场景:高并发订单.秒杀-- apache压力测试 Mysql锁详解 语法 加锁:LOCK TABLE 表名1 READ|WRITE, 表名2 READ|WRITE .................. 解锁:UNLOCK TABLES Read:读锁|共享锁 : 所有的客户端只能读这个表不能写这个表 Write:写锁|排它锁: 所有当前锁定客户端可以操作这个表,其他客户端只能阻塞 注意:在锁表的过程中只能操作被锁定的表,如果要操作其他表,必须把所有要操作的表都锁定起来!! PH

PHP之文件锁

1 <?php 2 3 4 /* 5 'r' 只读模式打开 ,指针指向文件头 6 'r+' 读写模式打开 ,文件指针指向文件头 7 'w' 置空写 忽略文件中的内容,如果文件不存在则创建 8 'w+' 置空读写模式 ,忽略文件中的内容, 如果文件不存在则创建 9 'a' 写入 ,将文件指针指向文件尾部 如果不存在则创建文件 10 'a+' 读写 将文件指针指向文件尾部, 如果不存在则创建 11 'x' 创建并以写入方式打开,将文件指针指向文件头 12 13 */ 14 $path = 'D:/1

php里面的文件锁

通过使用ab做并发执行的时候,发现库存减少不一致,为什么呢? 答:主要是由于并发的时候,多个php程序去操作了同一个资源,这个时候造成资源的抢夺,数据不一致.为了解决这个问题,可以使用php里面的文件锁来实现.在多个php程序操作某一个资源的时候,需要先去获取这个锁资源,只有获取到锁的程序才有权限去操作资源.当操作完成后,释放锁资源,使得别的程序能再次去抢夺锁资源 1.创建一把锁,一个文本文件,随意命名,lock.txt $key = fopen('lock.txt','r'); do{ $lo