[C #. Net] two methods to determine whether a file is occupied,

Source: Internet
Author: User

[C #. Net] two methods to determine whether a file is occupied,

During the development of the production line Tool today, it is found that log files are frequently occupied. You can browse the Internet to find the simplest code (API or FileStream.

Method 1: API

1 using System. IO; 2 using System. runtime. interopServices; 3 4 [DllImport ("kernel32.dll")] 5 public static extern IntPtr _ lopen (string lpPathName, int iReadWrite); 6 7 [DllImport ("kernel32.dll")] 8 public static extern bool CloseHandle (IntPtr hObject); 9 10 public const int OF_READWRITE = 2; 11 public const int OF_SHARE_DENY_NONE = 0x40; 12 public readonly IntPtr HFILE_ERROR = new IntPtr (-1); 13 pr Ivate void button#click (object sender, EventArgs e) 14 {15 string vFileName = @ "c: \ temp \ temp.bmp"; 16 if (! File. Exists (vFileName) 17 {18 MessageBox. Show ("the File does not exist! "); 19 return; 20} 21 IntPtr vHandle = _ lopen (vFileName, OF_READWRITE | of_assist_deny_none); 22 if (vHandle = HFILE_ERROR) 23 {24 MessageBox. show ("file occupied! "); 25 return; 26} 27 CloseHandle (vHandle); 28 MessageBox. Show (" not occupied! "); 29}

Method 2: FileStream

1 public static bool IsFileInUse (string fileName) 2 {3 bool inUse = true; 4 5 FileStream fs = null; 6 try 7 {8 9 fs = new FileStream (fileName, FileMode. open, FileAccess. read, 10 11 FileShare. none); 12 13 inUse = false; 14} 15 catch 16 {17} 18 finally 19 {20 if (fs! = Null) 21 22 fs. Close (); 23} 24 return inUse; // true indicates that it is in use, false indicates that 25 is not used}

Project code section (VB. Net)

1 Sub Prepare () 2 If File. exists (logRW) Then File. delete (logRW) 3 Dim bflag As Boolean = False 4 Try 5 Do 6 Shell ("CMD.exe/C RW.exe/Command = LimitA. rw/LogFile = LimitA. log ", AppWinStyle. hide, True, 5000) 7 Threading. thread. sleep (1000) 8 While (IsFileInUse ("LimitA. log ") 9 Threading. thread. sleep (2000) 10 End While11 12 If File. exists (logRW) Then13 Dim All As String = My. computer. fileSystem. readAllText (logRW) 14' check whether the value of 0x01 of LogRW is 0x0815 If All. contains ("Read EC Byte 0x01 = 0x80") Then16 bflag = True17 End If18 End If19 Loop Until bflag20 21 Using sr As New StreamReader (logRW) 22 Do Until sr. endOfStream23 Dim s As String = sr. readLine24 If s. contains ("Set Environment RwLOCAL3") Then25 'set Environment RwLOCAL3 = 4608 (DEC) 26 LimitA = CDbl (s. split (New String () {"=", "("}, StringSplitOptions. removeEmptyEntries) (1) 27 Console. writeLine ("Limit Current:" & LimitA) 28 LogStr = LogStr & vbCrLf & "Limit Current:" & LimitA29 Exit Do30 End If31 Loop32 End Using33 34 Catch ex As Exception35 Console. writeLine (ex. message & Err. description) 36 Environment. exit (1) 37 End Try38 End Sub39 40 Function IsFileInUse (ByVal fileName As String) 41 Dim inUse As Boolean = True42 Dim fs As FileStream = Nothing43 Try44 fs = New FileStream (fileName, fileMode. open, FileAccess. read, FileShare. none) 45 inUse = False46 Catch ex As prediction47 48 Finally49 If (fs IsNot Nothing) Then50 fs. close () 51 End If52 End Try53 Return inUse54 End Function

 

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.