C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » .NET »

Locking issue with File Operations...


Posted Date: 16 Dec 2008      Posted By: Vijay Kumar Vemula      Member Level: Gold     Points: 1   Responses: 2



Hi all,

I am writing below code to access a .txt file on the hard disk.

public static FileStream OpenReadWrite(String PathNm, bool bExclusiveWrite)
{
int Sharing = FILE_SHARE_READ;

if (!bExclusiveWrite)
Sharing |= FILE_SHARE_WRITE;

SafeFileHandle FileHandle = CreateFile(PathNm, GENERIC_WRITE | GENERIC_READ, Sharing, IntPtr.Zero, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero);

if (FileHandle.IsInvalid)
Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error());

FileStream fs = new FileStream(FileHandle, FileAccess.ReadWrite);

fs.Seek(fs.Length, SeekOrigin.Begin);

return fs;
}


[DllImport("Kernel32.dll", CharSet=CharSet.Ansi, EntryPoint = "CreateFile")]
private static extern SafeFileHandle CreateFile(System.String PathNm, uint openmode, int fileshare, IntPtr SA, int openflags, int attribs, IntPtr templFile);

private static uint GENERIC_READ = 0x80000000;
private static uint GENERIC_WRITE = 0x40000000;
private static int FILE_SHARE_READ = 0x00000001;
private static int FILE_SHARE_WRITE = 0x00000002;
private static int OPEN_ALWAYS = 4;
private static int OPEN_EXISTING = 3;
private static int FILE_ATTRIBUTE_NORMAL = 0x00000080;


I am able to access the text file with the above written code without any issue. But, where as if I want to access the file in sharing mode (while another process is using it), I am getting exception. The exception is as follows.

Invalid handle.
Parameter name: handle


Can anybody tell me what is the quick fix for this issue?

I will be waiting for your reply.... as it is urgent issue, I want its solution ASAP.

Thanks inadvance.





Responses

Author: MOHAN BABU DODDAPANENI    16 Dec 2008Member Level: SilverRating: 2 out of 52 out of 5     Points: 3

I could see
if (!bExclusiveWrite)
Sharing |= FILE_SHARE_WRITE;

Is ! intensional.. I guess it should be

if ( bExclusiveWrite )
Sharing |= FILE_SHARE_WRITE;


is GENERIC_WRITE and GENERIC_READ in CreateFile both required for you ?

If you are opening the file just for reading..then have just read

And one more thing... don't forget to close the handle using CloseHandle



Author: Amit Shah    16 Dec 2008Member Level: GoldRating: 2 out of 52 out of 5     Points: 6

close all handles after read write.

when your shared file read then no problem but if there ar writing then use lock unlock method which is provied by .net framework..

let me know if find problem !!!!!

------------------
Hi all,

I am writing below code to access a .txt file on the hard disk.

public static FileStream OpenReadWrite(String PathNm, bool bExclusiveWrite)
{
int Sharing = FILE_SHARE_READ;

if (!bExclusiveWrite)
Sharing |= FILE_SHARE_WRITE;

SafeFileHandle FileHandle = CreateFile(PathNm, GENERIC_WRITE | GENERIC_READ, Sharing, IntPtr.Zero, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero);

if (FileHandle.IsInvalid)
Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error());

FileStream fs = new FileStream(FileHandle, FileAccess.ReadWrite);

fs.Seek(fs.Length, SeekOrigin.Begin);

return fs;
}


[DllImport("Kernel32.dll", CharSet=CharSet.Ansi, EntryPoint = "CreateFile")]
private static extern SafeFileHandle CreateFile(System.String PathNm, uint openmode, int fileshare, IntPtr SA, int openflags, int attribs, IntPtr templFile);

private static uint GENERIC_READ = 0x80000000;
private static uint GENERIC_WRITE = 0x40000000;
private static int FILE_SHARE_READ = 0x00000001;
private static int FILE_SHARE_WRITE = 0x00000002;
private static int OPEN_ALWAYS = 4;
private static int OPEN_EXISTING = 3;
private static int FILE_ATTRIBUTE_NORMAL = 0x00000080;


I am able to access the text file with the above written code without any issue. But, where as if I want to access the file in sharing mode (while another process is using it), I am getting exception. The exception is as follows.

Invalid handle.
Parameter name: handle


Can anybody tell me what is the quick fix for this issue?

I will be waiting for your reply.... as it is urgent issue, I want its solution ASAP.

Thanks inadvance.



Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : Third Party Controls
Previous : Windows form application Design
Return to Discussion Forum
Post New Message
Category: .NET

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use