Determine whether a file is open in C # WinForm

Source: Internet
Author: User

Recently do a topic, require WinForm client can save the received experimental data to a file (such as a. txt file), and can open the saved experimental data document, when the data file has been opened, to prompt the file has been opened, that is, how to determine whether the file is open, and then give the appropriate prompt. Finding the data found that C # itself is difficult to determine whether a file is opened, the relevant data to be used to WINDOWSAPI to achieve, this method is arguably possible, but perhaps I did not fully understand the meaning of the relevant function and return value, so that the file has not been opened to judge, The relevant procedures and questions are given below:

public class Filestaues    {        [DllImport ("kernel32.dll")]        private static extern IntPtr _lopen (string lppathname, int ireadwrite);//intptr equals long type        [DllImport ("kernel32.dll")]        private static extern bool CloseHandle (IntPtr hobject);        Private Const int of_write = 2;        Private Const int of_share_deny_none = 0x40;        private static readonly IntPtr hfile_error = new IntPtr (964);        public static int Fileisopen (string filefullname)        {            if (! File.exists (filefullname))            {//does not exist file                return-1;            }            The value of handle is always not-1, so it cannot be returned 1, it cannot be determined that the file is open, this place is problematic            IntPtr handle = _lopen (Filefullname, Of_write | Of_share_deny_none);            if (handle = = Hfile_error)            {//File already open                return 1;            }            CloseHandle (handle);            return 0;        }    }
Related calls:

private int saveopenresult; Saveopenresult = Filestaues.fileisopen (receivefilename); if (Saveopenresult = = 0)     System.Diagnostics.Process.Start (receivefilename); else if (Saveopenresult = =-1)     throw new Exception ("file does not exist ...") ; else if (Saveopenresult = =-1)     throw new Exception ("File is open ...");  The. txt save file appears when the Open button is pressed, but the. txt save file appears after you press the Open button, and the
I hope someone can point out the mistake of this method, thank you!!!


In order to be able to prompt to save the file has been opened, you need to find another way ...

through Find C # Open File information, C # Open file to use

System.Diagnostics.Process.Start (file path \ filename);

Statement to open, which is to use the process to open the file, the equivalent of opening a file is open a process, then can determine whether the relevant process exits to determine if the file is open? After testing, it is possible, the procedure is as follows:

<span style= "font-family:arial, Helvetica, Sans-serif;" >system.diagnostics.process opensavefileproc;//Specify process </span><pre name= "code" class= "CSharp" >private void Opensavebutton01_click (object sender, EventArgs e)//Open save file        {            try            {                if (! File.exists (file path \ filename))                    throw new Exception ("file does not exist ...");                if (Opensavefileproc = = NULL | | opensavefileproc.hasexited = = true)                    Opensavefileproc = System.Diagnostics.Process.Start (file path \ filename);                else throw new Exception ("File is open ...");            catch (System.Exception ex)            {                MessageBox.Show (ex. Message, "error hint");            }        }

Results


The first time you press the "open" button, open the file



After pressing the "open" button again, it is determined that the RCF.txt file has been opened and an error prompt is given, and the RCF.txt file box will not appear again.


Personal original, reproduced please specify the source!!!






Determine whether a file is open in C # WinForm

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.