myJavaBlog

java世界
随笔 - 43, 文章 - 0, 评论 - 2, 引用 - 0
数据加载中……

教你使用C# 实现文件锁

C#代码  收藏代码
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using System.IO;  
  10.   
  11. namespace FileTokenNotWork  
  12. {  
  13.     public partial class Form1 : Form  
  14.     {  
  15.         FileStream fs = null;  
  16.         int count = 0;  
  17.         public Form1()  
  18.         {  
  19.             InitializeComponent();  
  20.         }  
  21.   
  22.         private void btnUpdate_Click(object sender, EventArgs e)  
  23.         {  
  24.             OpenFileDialog ofd = new OpenFileDialog();  
  25.             ofd.ShowDialog();  
  26.             filedirtectory.Text = ofd.FileName;  
  27.   
  28.         }  
  29.   
  30.         private void button1_Click(object sender, EventArgs e)  
  31.         {  
  32.             count++;  
  33.             if (count > 1)  
  34.             {  
  35.                 MessageBox.Show("Do not do it again.","Warning",MessageBoxButtons.OK,MessageBoxIcon.Warning);  
  36.                 return;  
  37.             }  
  38.             try  
  39.             {  
  40.                 if (string.IsNullOrEmpty(filedirtectory.Text))  
  41.                 {  
  42.                     MessageBox.Show("Not have the file", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);  
  43.                     count--;  
  44.                     return;  
  45.                 }  
  46.                 fs = new FileStream(filedirtectory.Text, FileMode.Open, FileAccess.Read, FileShare.None);  
  47.             }  
  48.             catch (Exception)  
  49.             {  
  50.                 fs.Close();  
  51.             }  
  52.         }  
  53.   
  54.         private void button2_Click(object sender, EventArgs e)  
  55.         {  
  56.             count--;  
  57.             fs.Close();  
  58.         }  
  59.     }  
  60. }  

 

C#代码  收藏代码
  1. namespace FileTokenNotWork  
  2. {  
  3.     partial class Form1  
  4.     {  
  5.         /// <summary>  
  6.         /// Required designer variable.  
  7.         /// </summary>  
  8.         private System.ComponentModel.IContainer components = null;  
  9.   
  10.         /// <summary>  
  11.         /// Clean up any resources being used.  
  12.         /// </summary>  
  13.         /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>  
  14.         protected override void Dispose(bool disposing)  
  15.         {  
  16.             if (disposing && (components != null))  
  17.             {  
  18.                 components.Dispose();  
  19.             }  
  20.             base.Dispose(disposing);  
  21.         }  
  22.  
  23.         #region Windows Form Designer generated code  
  24.   
  25.         /// <summary>  
  26.         /// Required method for Designer support - do not modify  
  27.         /// the contents of this method with the code editor.  
  28.         /// </summary>  
  29.         private void InitializeComponent()  
  30.         {  
  31.             this.btnUpdate = new System.Windows.Forms.Button();  
  32.             this.openFileDialog = new System.Windows.Forms.OpenFileDialog();  
  33.             this.filedirtectory = new System.Windows.Forms.TextBox();  
  34.             this.groupBox1 = new System.Windows.Forms.GroupBox();  
  35.             this.groupBox2 = new System.Windows.Forms.GroupBox();  
  36.             this.button1 = new System.Windows.Forms.Button();  
  37.             this.button2 = new System.Windows.Forms.Button();  
  38.             this.groupBox1.SuspendLayout();  
  39.             this.groupBox2.SuspendLayout();  
  40.             this.SuspendLayout();  
  41.             //   
  42.             // btnUpdate  
  43.             //   
  44.             this.btnUpdate.Location = new System.Drawing.Point(175, 20);  
  45.             this.btnUpdate.Name = "btnUpdate";  
  46.             this.btnUpdate.Size = new System.Drawing.Size(75, 23);  
  47.             this.btnUpdate.TabIndex = 0;  
  48.             this.btnUpdate.Text = "submit";  
  49.             this.btnUpdate.UseVisualStyleBackColor = true;  
  50.             this.btnUpdate.Click += new System.EventHandler(this.btnUpdate_Click);  
  51.             //   
  52.             // openFileDialog  
  53.             //   
  54.             this.openFileDialog.FileName = "openFileDialog";  
  55.             //   
  56.             // filedirtectory  
  57.             //   
  58.             this.filedirtectory.Location = new System.Drawing.Point(18, 22);  
  59.             this.filedirtectory.Name = "filedirtectory";  
  60.             this.filedirtectory.Size = new System.Drawing.Size(134, 21);  
  61.             this.filedirtectory.TabIndex = 1;  
  62.             //   
  63.             // groupBox1  
  64.             //   
  65.             this.groupBox1.Controls.Add(this.filedirtectory);  
  66.             this.groupBox1.Controls.Add(this.btnUpdate);  
  67.             this.groupBox1.Location = new System.Drawing.Point(12, 12);  
  68.             this.groupBox1.Name = "groupBox1";  
  69.             this.groupBox1.Size = new System.Drawing.Size(268, 65);  
  70.             this.groupBox1.TabIndex = 2;  
  71.             this.groupBox1.TabStop = false;  
  72.             this.groupBox1.Text = "Find file directory";  
  73.             //   
  74.             // groupBox2  
  75.             //   
  76.             this.groupBox2.Controls.Add(this.button2);  
  77.             this.groupBox2.Controls.Add(this.button1);  
  78.             this.groupBox2.Location = new System.Drawing.Point(12, 97);  
  79.             this.groupBox2.Name = "groupBox2";  
  80.             this.groupBox2.Size = new System.Drawing.Size(268, 98);  
  81.             this.groupBox2.TabIndex = 3;  
  82.             this.groupBox2.TabStop = false;  
  83.             this.groupBox2.Text = "lock file";  
  84.             //   
  85.             // button1  
  86.             //   
  87.             this.button1.Location = new System.Drawing.Point(28, 37);  
  88.             this.button1.Name = "button1";  
  89.             this.button1.Size = new System.Drawing.Size(75, 23);  
  90.             this.button1.TabIndex = 0;  
  91.             this.button1.Text = "lock";  
  92.             this.button1.UseVisualStyleBackColor = true;  
  93.             this.button1.Click += new System.EventHandler(this.button1_Click);  
  94.             //   
  95.             // button2  
  96.             //   
  97.             this.button2.Location = new System.Drawing.Point(144, 37);  
  98.             this.button2.Name = "button2";  
  99.             this.button2.Size = new System.Drawing.Size(75, 23);  
  100.             this.button2.TabIndex = 1;  
  101.             this.button2.Text = "release";  
  102.             this.button2.UseVisualStyleBackColor = true;  
  103.             this.button2.Click += new System.EventHandler(this.button2_Click);  
  104.             //   
  105.             // Form1  
  106.             //   
  107.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);  
  108.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;  
  109.             this.ClientSize = new System.Drawing.Size(290, 204);  
  110.             this.Controls.Add(this.groupBox2);  
  111.             this.Controls.Add(this.groupBox1);  
  112.             this.Name = "Form1";  
  113.             this.Text = "LockFileTool";  
  114.             this.groupBox1.ResumeLayout(false);  
  115.             this.groupBox1.PerformLayout();  
  116.             this.groupBox2.ResumeLayout(false);  
  117.             this.ResumeLayout(false);  
  118.   
  119.         }  
  120.  
  121.         #endregion  
  122.   
  123.         private System.Windows.Forms.Button btnUpdate;  
  124.         private System.Windows.Forms.OpenFileDialog openFileDialog;  
  125.         private System.Windows.Forms.TextBox filedirtectory;  
  126.         private System.Windows.Forms.GroupBox groupBox1;  
  127.         private System.Windows.Forms.GroupBox groupBox2;  
  128.         private System.Windows.Forms.Button button2;  
  129.         private System.Windows.Forms.Button button1;  
  130.     }  
  131. }  

 

C#代码  收藏代码
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Windows.Forms;  
  5.   
  6. namespace FileTokenNotWork  
  7. {  
  8.     static class Program  
  9.     {  
  10.         /// <summary>  
  11.         /// The main entry point for the application.  
  12.         /// </summary>  
  13.         [STAThread]  
  14.         static void Main()  
  15.         {  
  16.             Application.EnableVisualStyles();  
  17.             Application.SetCompatibleTextRenderingDefault(false);  
  18.             Application.Run(new Form1());  
  19.         }  
  20.     }  
  21. }  
    (转载请注明出处:[url=http://www.k8764.com]博彩通[/url]

    [url=http://www.5sfd.com]e世博[/url]

posted on 2012-04-19 09:43 夏凡 阅读(849) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: