""Peter Huang" [MSFT]" <v-phuang@online.microsoft.com> wrote in message
news:nRNEZxKBHHA.4020@TK2MSFTNGXA01.phx.gbl...
> Hi Marek,
>
> Currently this is the standard in application to detect if one file is in
> use or not.
> If you feel the .NET way is somewhat slow, I think you may try the Win32
> API directly.
> using System;
> using System.Collections.Generic;
> using System.ComponentModel;
> using System.Data;
> using System.Drawing;
> using System.Text;
> using System.Windows.Forms;
> using System.Runtime.InteropServices;
> namespace WindowsApplication19
> {
> public partial class Form1 : Form
> {
> public Form1()
> {
> InitializeComponent();
> }
> //
> // CreateFile constants
> //
> const uint FILE_SHARE_READ = 0x00000001;
> const uint FILE_SHARE_WRITE = 0x00000002;
> const uint FILE_SHARE_DELETE = 0x00000004;
> const uint OPEN_EXISTING = 3;
>
> const uint GENERIC_READ = (0x80000000);
> const uint GENERIC_WRITE = (0x40000000);
>
> const uint FILE_FLAG_NO_BUFFERING = 0x20000000;
> const uint FILE_READ_ATTRIBUTES = (0x0080);
> const uint FILE_WRITE_ATTRIBUTES = 0x0100;
> const uint ERROR_INSUFFICIENT_BUFFER = 122;
>
> [DllImport("kernel32.dll", SetLastError = true)]
> static extern IntPtr CreateFile(
> string lpFileName,
> uint dwDesiredAccess,
> uint dwShareMode,
> IntPtr lpSecurityAttributes,
> uint dwCreationDisposition,
> uint dwFlagsAndAttributes,
> IntPtr hTemplateFile);
>
> [DllImport("kernel32.dll", SetLastError = true)]
> static extern int CloseHandle(IntPtr hObject);
> private void button1_Click(object sender, EventArgs e)
> {
> IntPtr hHandle = CreateFile(@"c:\test.doc", GENERIC_READ, 0,
> IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
> if (hHandle.ToInt32() == -1)
> MessageBox.Show("File in Use");
> CloseHandle(hHandle);
> }
> }
> }
>
> Best regards,
>
> Perter Huang
> Microsoft Online Partner Support
>
> Get Secure! -
www.microsoft.com/security > This posting is provided "AS IS" with no warranties, and confers no
> rights.
>