Groups | Blog | Home
all groups > dotnet interop > november 2006 >

dotnet interop : How to dected if file is used by another process


Marek Suski
11/10/2006 12:00:00 AM
Hi,

I am looking for a fast Win32 code solution for detecting if file is used by
another process (aka locked). Right now I am using following code but it
throws errors that I catch. Is there something faster and cleaner?

/// <summary>
/// Determines whether [is file locked] [the specified path to file].
/// </summary>
/// <param name="pathToFile">The path to file.</param>
/// <returns>
/// <c>true</c> if [is file locked] [the specified path to file];
otherwise, <c>false</c>.
/// </returns>
public static bool IsFileLocked(string pathToFile) {
string pathToLockFile;
Stream stream = null;
bool isFileLocked;

pathToLockFile = pathToFile + "_lock";

try {
stream = System.IO.File.Open(pathToLockFile, FileMode.Create,
FileAccess.Write, FileShare.None);
stream.Close();
System.IO.File.Delete(pathToLockFile);
isFileLocked = false;
}catch(Exception exp) {
Debug.WriteLine(exp.ToString());
isFileLocked = true;
}

return isFileLocked;
}

Best regards

Marek Suski

v-phuang NO[at]SPAM online.microsoft.com (
11/10/2006 12:00:00 AM
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.
Marek Suski
11/10/2006 12:08:39 PM
Thank you, this is was I was looking for :)


[quoted text, click to view]

AddThis Social Bookmark Button