`

C# 实现文件锁

    博客分类:
  • C#
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());
        }
    }
}

 

0
1
分享到:
评论

相关推荐

    C# WinForm文件管理系统(终结版)

    学生个人文件管理系统 v2.0 A.声明: 1.非源代码。 2.有些用户没有设置自己的数据库服务器就直接登录或者注册,这样是不会成的, 请选择自定义服务器,然后填入自己的IP,数据库用户名以及密码,因为默认服 ...

    C# 实现局域网视频监控(Socket 编程)

    本系统以 C/S 架构采用 Socket 技术开发,可以运用本软件在局域网中实施监控管理。服务器端可以实时观察远程...除了这些,服务器端可以执行更高级的操作:即浏览客户端文件系统,并可以任意拷贝指定的客户端目录。

    C#锁住文件的操作步骤

    首先大家需要到码云里面找到如下图所示的文件锁的项目,如下图所示 下载下来以后导入到Visual Studio中,如下图所示,这个项目是用C#写的,所以你可以直接阅读的 接着运行这个C#项目,就可以看到如下图所示的加索...

    C#代码实现服务程序安装卸载

    程序演示了如何通过代码(不使用InstallUtil.exe)来对C#开发的Windows系统服务程序进行安装和卸载,使用了单独编写的安装程序,安装和卸载在线程中进行,带进度条,界面不锁死。  代码中的解决方案包括2个项目:...

    c# repeater 锁表头

    c# repeater 锁表头 一、将表头与内容分离。 此方法的思路其实很简单,就是将表头放在一个表格中,将表单内容repeater放在一个DIV中 ,再将DIV的样式高度设为定值,并将此div设置为允许出现纵向滚动条。这种方法...

    vb.net C#线程锁超时控制代码

    ManualResetEvent是一个超时等待的线程锁,如果超时返回false,接收指令显示true,但是没法实现在超时后让他继续等待,这种需求我们可以用在以下场景: 控制线程超时方法 1.[主线程]:请求方发送请求,立即创建超时等待...

    明日科技《C#示例源代码》(5-8)

    实例073 使用C#操作INI文件 255 实例074 使用C#操作XML文件 257 实例075 创建PDF文档 261 实例076 批量将Word文档转换为HTML网页 263 实例077 Word目录提取工具 265 实例078 文件批量解压缩 269 ...

    C#微软培训资料

    15.4 接口的实现 .182 15.5 抽象类与接口 .195 15.6 小 结 .196 第十六章 组织应用程序 .198 16.1 基 本 概 念 .198 16.2 使用名字空间 .200 16.3 使用指示符 .203 16.4 程 序 示 例 .206 16.5 小 ...

    明日科技《C#示例源代码》(9-12)

    实例073 使用C#操作INI文件 255 实例074 使用C#操作XML文件 257 实例075 创建PDF文档 261 实例076 批量将Word文档转换为HTML网页 263 实例077 Word目录提取工具 265 实例078 文件批量解压缩 269 ...

    明日科技C#示例源码.part02

    实例073 使用C#操作INI文件 255 实例074 使用C#操作XML文件 257 实例075 创建PDF文档 261 实例076 批量将Word文档转换为HTML网页 263 实例077 Word目录提取工具 265 实例078 文件批量解压缩 269 ...

    在一小时内学会 C#(txt版本)

    和 C++ 有所区别的是,C# 中没有单独的声明(头)和实现(CPP)文件。所有代码(类声明和实现)都放在扩展名为 cs 的单一文件中。 看看 C# 中的 Hello World 程序。 复制内容到剪贴板 代码: using System; ...

    Windows中使用C#为文件夹和文件编写密码锁的示例分享

    用C#语言实现一个文件夹锁的程序,网上类似的“xxx文件夹xxx”软件很多,但是基本上都是C/C++语言实现的,且都没有提供源码(这个可以理解,毕竟是加密程序,不应该泄露源码)。 程序的基本原理是:用C#语言重命名...

    C#示例(配套光盘源码)第五部分

    实例074 使用C#操作XML文件 257 实例075 创建PDF文档 261 实例076 批量将Word文档转换为HTML网页 263 实例077 Word目录提取工具 265 实例078 文件批量解压缩 269 第8章 网络开发应用 实例079 局域网...

    明日科技C#示例源码.part04

    实例073 使用C#操作INI文件 255 实例074 使用C#操作XML文件 257 实例075 创建PDF文档 261 实例076 批量将Word文档转换为HTML网页 263 实例077 Word目录提取工具 265 实例078 文件批量解压缩 269 ...

    C#示例(配套光盘源码)第二部分

    实例074 使用C#操作XML文件 257 实例075 创建PDF文档 261 实例076 批量将Word文档转换为HTML网页 263 实例077 Word目录提取工具 265 实例078 文件批量解压缩 269 第8章 网络开发应用 实例079 局域网...

    明日科技C#示例源码.part03

    实例073 使用C#操作INI文件 255 实例074 使用C#操作XML文件 257 实例075 创建PDF文档 261 实例076 批量将Word文档转换为HTML网页 263 实例077 Word目录提取工具 265 实例078 文件批量解压缩 269 ...

    明日科技C#示例源码

    实例074 使用C#操作XML文件 257 实例075 创建PDF文档 261 实例076 批量将Word文档转换为HTML网页 263 实例077 Word目录提取工具 265 实例078 文件批量解压缩 269 第8章 网络开发应用 实例079...

    基于C#的点菜系统(源码+数据库+配置说明文件).zip

    (2)实例执行文件位置:TM\06\MrCy\MrCy\bin\Debug\ MrCy.exe (3)本系统共分为三种操作权限,分别为超级管理员、经理、一般用户: 一般用户: 一般用户只可以对辅助工具菜单、系统设置菜单、窗口布局菜单及帮助...

    C#120问(重点难点)

    摘抄了里面两个问题,还有120个,真的很不错 1.维护数据库的完整性、一致性、...SqlServer自动锁定特定记录、字段或文件,防止用户访问,以维护数据安全或防止并发数据操作问题,锁可以保证事务的完整性和并发性。

    C#示例(配套光盘源码)第四部分

    实例074 使用C#操作XML文件 257 实例075 创建PDF文档 261 实例076 批量将Word文档转换为HTML网页 263 实例077 Word目录提取工具 265 实例078 文件批量解压缩 269 第8章 网络开发应用 实例079 局域网...

Global site tag (gtag.js) - Google Analytics