Akai

Hi all

im writing a Windows Service program which checks a certain directory in my computer to see if there's any new files. If there's a new file created in the directory, i will rename the file. However if the HUGE file is being copied into my directory, i would need to check if the file is being used in any process. Only if the file is idle, i will call my code to rename it. Im using FileSystemAccessRule etc to check for the file access. I have posted my codes below.. can some1 help me coz im still getting IO.Exception: file is used by another process error. I got the CanDelete function from some sites hehe.

public void FileCreated(object sender, FileSystemEventArgs e1)

{

if (Compare.IndexOf(e1.Name, "Manual Recording", CompareOptions.IgnoreCase) != -1)

{

while (CanDelete(e1.FullPath))

{

System.Threading.Thread.Sleep(5000);

}

System.Threading.Thread.Sleep(5000);

//Renaming the filename

System.IO.File.Move(e1.FullPath, @"C:\Users\Public\Rename.txt" );

}

}

private static bool CanDelete(string fname)

{

System.Security.Principal.WindowsIdentity WinIdentity = System.Security.Principal.WindowsIdentity.GetCurrent();

System.Security.Principal.WindowsPrincipal WinPrincipal = new System.Security.Principal.WindowsPrincipal(WinIdentity);

bool AllowDelete = false;

FileInfo fi = new FileInfo(fname);

FileSecurity sec = fi.GetAccessControl();

foreach (FileSystemAccessRule rule in sec.GetAccessRules(true, true, typeof System.Security.Principal.NTAccount)))

{

if (WinPrincipal.IsInRole(rule.IdentityReference.Value))

{

if (((int)rule.FileSystemRights & (int)FileSystemRights.Delete) > 0)

{

if (rule.AccessControlType == AccessControlType.Allow)

{

AllowDelete = false;

}

else if (rule.AccessControlType == AccessControlType.Deny)

{

return true;

}

}

}

}

return AllowDelete;

}

Thank You



Re: Visual C# General How to check if im able to rename a file in a directory?

Peter Ritchie

The only way to reliably detect if a file is in use is to catch the exception.






Re: Visual C# General How to check if im able to rename a file in a directory?

Citizen on the earth

Hello Akai,

Based on my understanding, you want to determine if one file is locked by another process.

For your concern, just as Peter suggested, you can use exception to get this done.
Use your .Net application to access this file, if this file is locked, then an exception will generate, in the exception handler, you can determine that this file is locked by another process.

Citizens on the earth





Re: Visual C# General How to check if im able to rename a file in a directory?

Akai

Hi all

thxs for all the advice. I found the following function to check if a file is being locked.

The link is as follows:

http://www.codeproject.com/cs/files/C__FileWatcher.asp

protected virtual bool IsFileLocked(FileInfo file)
{
FileStream stream = null;

try
{
stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
}
catch(IOException ex)
{
//the file is unavailable because it is:
//still being written to
//or being processed by another thread
//or does not exist (has already been processed)
return true;
}
finally
{
if(stream != null)
{
stream.Close();
}
}

//file is not locked
return false;
}

Thank You





Re: Visual C# General How to check if im able to rename a file in a directory?

Matthew Watson

Note that if you use a "using" statement when opening the file (which is recommended practice) the code is much more succinct:



protected virtual bool IsFileLocked(FileInfo file)
{
try
{
using (file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
return false;
}
}

catch (IOException)
{
return true;
}
}