使用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 = ;
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 > )
{
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(, );
this.btnUpdate.Name = "btnUpdate";
this.btnUpdate.Size = new System.Drawing.Size(, );
this.btnUpdate.TabIndex = ;
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(, );
this.filedirtectory.Name = "filedirtectory";
this.filedirtectory.Size = new System.Drawing.Size(, );
this.filedirtectory.TabIndex = ;
//
// groupBox1
//
this.groupBox1.Controls.Add(this.filedirtectory);
this.groupBox1.Controls.Add(this.btnUpdate);
this.groupBox1.Location = new System.Drawing.Point(, );
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(, );
this.groupBox1.TabIndex = ;
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(, );
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(, );
this.groupBox2.TabIndex = ;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "lock file";
//
// button1
//
this.button1.Location = new System.Drawing.Point(, );
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(, );
this.button1.TabIndex = ;
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(, );
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(, );
this.button2.TabIndex = ;
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(, );
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# 实现文件锁的更多相关文章
- php文件锁
前言 1.锁机制之所以存在是因为并发问题导致的资源竞争,为了确保操作的有效性和完整性,可以通过锁机制将并发状态转换成串行状态.作为锁机制中的一种,PHP 的文件锁也是为了应对资源竞争.假设一个应用场景 ...
- JAVA NIO简介-- Buffer、Channel、Charset 、直接缓冲区、分散和聚集、文件锁
IO 是主存和外部设备 ( 硬盘.终端和网络等 ) 拷贝数据的过程. IO 是操作系统的底层功能实现,底层通过 I/O 指令进行完成. Java标准io回顾 在Java1.4之前的I/O系统中,提供 ...
- Linux文件锁flock
Linux文件锁flock 在多个进程同时操作同一份文件的过程中,很容易导致文件中的数据混乱,需要锁操作来保证数据的完整性,这里介绍的针对文件的锁,称之为“文件锁”-flock. flock,建议性锁 ...
- JAVA基础知识之NIO——Buffer.Channel,Charset,Channel文件锁
NIO机制 NIO即NEW IO的意思,是JDK1.4提供的针对旧IO体系进行改进之后的IO,新增了许多新类,放在java.nio包下,并对java.io下许多类进行了修改,以便使用与nio. 在ja ...
- Linux 文件锁
当多个进程同时访问操作同一个文件时,我们怎么保证文件数据的正确性. linux通常采用的方法是文件上锁,来避免共享资源的产生竞争状态. 文件锁包括建议性锁和强制性的锁: 建议性的锁 :顾名思义,相对温 ...
- php中并发读写文件冲突的解决方案(文件锁应用示例)
PHP(外文名: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言.语法吸收了C语言.Java和Perl的特点,入门门槛较低,易于学习,使用广泛,主要适 ...
- php原子操作,文件锁flock,数据库事务
php原子操作,文件锁flock,数据库事务 php没有继承posix标准支持的unix锁,只封装了一个linux系统调用flock(信号量也能做成锁),按理也是可以使用锁机制的,虽然效率低一点.ph ...
- linux文件锁
http://blog.chinaunix.net/uid-25324849-id-3077304.html 在SHELL中实现文件锁,有两种简单的方式.(1)一是利用普通文件,在脚本启动时检查特定文 ...
- linux使用flock文件锁解决crontab冲突问题
* * * * * flock -xn /dev/shm/redis.lock -c "/usr/local/bin/redis-server" 可以用flock命令,配合使用rs ...
- Linux同步机制(二) - 条件变量,信号量,文件锁,栅栏
1 条件变量 条件变量是一种同步机制,允许线程挂起,直到共享数据上的某些条件得到满足. 1.1 相关函数 #include <pthread.h> pthread_cond_t cond ...
随机推荐
- TabHost Tab的添加和删除
TabHost 添加Tab项: tabhost = this.getTabHost(); TabSpec tabSpec = tabhost.newTabSpec("news"); ...
- FastDFS总结
前言 FastDFS主要解决互联网中小文件存储存储问题,例如图片,短视频,提供上传和下载功能,轻量级的设计,结构非常简单,主要包含三个角色客户端,Tracer服务,Storage服务.Tracer服务 ...
- 手势解锁自定义View
package com.rxx.view; import java.util.ArrayList; import java.util.List; import java.util.Timer; imp ...
- jquery阻止冒泡事件:$('span').bind("click",function(event){event.stopPropagation();})(有用源)
冒泡事件就是点击子节点,会向上触发父节点,祖先节点的点击事件. <body> <div id="content"> 外层div元素 <span> ...
- HDU4861:Couple doubi(费马小定理)
题意: 给出k个球和质数p,对每个球以公式val(i)=1^i+2^i+...+(p-1)^i (mod p)计算出它的价值,然后两个人轮流拿,最后拿到的球的总价值大的获胜,问我们先手是否获胜. 我们 ...
- 有关OOM KILLER的一些理解
Linux下有一种OOM KILLER 的机制,它会在系统内存耗尽的情况下,启用自己算法有选择性的kill 掉一些进程. 一.为什么会有OOM killer 当我们使用应用时,需要申请内存,即进行ma ...
- Java缓存学习之四:EhCache
EhCache 关键词:纯Java的进程内缓存框架.Java分布式缓存.缓存数据有两级:内存和磁盘.缓存数据会在虚拟机重启的过程中写入磁盘.是hibernate默认的缓存provider: Ehcac ...
- Spring SimpleJdbcTemplate batchUpdate() example
In this tutorial, we show you how to use batchUpdate() in SimpleJdbcTemplate class. See batchUpdate( ...
- 生成Base58格式的UUID(Hibernate Base64格式的UUID续)
Base58简介 Base58采用的字符集合为“123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ”,从这不难看出,Base58是纯数 ...
- CAAnimation解读
序言 CAAnimation是一个抽象类,遵循了CAMediaTiming协议和CAAction协议!我们不要直接使用CAAnimation类,而是使用其子类: CATransition:提供渐变效果 ...