Use C # To implement file lock,

Source: Internet
Author: User

Use C # To implement file lock,

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());        }    }}


Simple code, no explanation


C language ^ how to use

A1 = 0x01; // 0000 0001
A2 = 0x00; // 0000 0000
A3 = 0x03; // 0000 0011
A4 = 0x02; // 0000 0010

B1 = a1 ^ a2; // 0000 0001
B2 = a1 ^ a3; // 0000 0010
B3 = a1 ^ a4; // 0000 0011

^ XOR operator. The bitwise value is 0 and the difference is 1. See the example above.

//
Examples of simple and practical problems:
====================================
======= A ======= B =========
There are two circuits on the top. The two switches are a and B respectively. The opening status is \ [1], and the closing status is/[0].
If both circuits are enabled or disabled.
If a turns on [1], B turns off [0], and circuit 1 Powers on
=====================
If a disables [0], B enables [1], and circuit 2 powers on.
====================================
In summary, the circuit fails in the and B states simultaneously [0]. When a and B are different, the power is charged [1].

How to Use enum in C Language

The enumeration type in C language is actually an integer variable. It only combines a type of associated identifiers by Enumeration type to increase the readability and maintainability of the program.

(1) enumeration is a set. Elements (enumeration members) in the set are named Integer constants separated by commas.
(2) DAY is an identifier. It can be regarded as the name of the set and is an optional item, that is, an optional item.
(3) The default value of the first enumeration member is 0 of the integer type, and the value of the subsequent enumeration member is increased by 1 on the previous Member.
(4) You can manually set the enumerated Member values to customize integers in a certain range.
(5) Enumeration type is an alternative to preprocessing command # define.
(6) type definition should end with a semicolon.

Define Enumeration type
Enum YOURENUMTYPE
{
ID1, // if no additional value is specified, the first identifier is equal to the integer 0, followed by 1
ID2,
ID3 = 7,
....
IDn // The Last identifier is not followed by a comma
}; // Note that you must add this semicolon

Define enumerated Variables
Enum YOURENUMTYPE varname;
Assign varname = IDx to the enumerated variable;

A complete example
Enum FRUIT
{
APPLE,
PEAR,
ORANGE,
PEACH,
GRAPE,
BANANA
};

Enmu FRUIT myfruit = PEACH;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.