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;
}
}
----转载自http://chenhailong.iteye.com/blog/1138711 对此深表感谢。