473,386 Members | 1,754 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Bug in C# File access code?

I have a file that has been created using shared read/write access.
But C# code never is able to open this file for read and keeps
throwing "...cannot open file as it is being used by another
process...".

I verified that the file is indeed available for shared reading. here
is my code snippet. is there a bug in C# that i dont know, or is there
a bug in my code?

FileIOPermission perm = new FileIOPermission(PermissionState.None);

perm.SetPathList(System.Security.Permissions.FileI OPermissionAccess.Read,
logpath);

perm.Demand( ); // can we do shared reading? this call succeeds

System.IO.FileStream filestr = new
FileStream(logpath,System.IO.FileMode.Open,System. IO.FileAccess.Read);
// still throws

any hints?

-Vinayak
Nov 16 '05 #1
2 2076
You'll get the error if somebody else has the file opened exclusively - are
you sure your code is the only code accessing the file?

--Richard

"Vinayak Raghuvamshi" wrote:
I have a file that has been created using shared read/write access.
But C# code never is able to open this file for read and keeps
throwing "...cannot open file as it is being used by another
process...".

I verified that the file is indeed available for shared reading. here
is my code snippet. is there a bug in C# that i dont know, or is there
a bug in my code?

FileIOPermission perm = new FileIOPermission(PermissionState.None);

perm.SetPathList(System.Security.Permissions.FileI OPermissionAccess.Read,
logpath);

perm.Demand( ); // can we do shared reading? this call succeeds

System.IO.FileStream filestr = new
FileStream(logpath,System.IO.FileMode.Open,System. IO.FileAccess.Read);
// still throws

any hints?

-Vinayak

Nov 16 '05 #2
You are clearly confusing Code Access Permissions and Windows File sharing
permissions.
perm.Demand( ); // can we do shared reading? this call succeeds This doesn't demand shared reading permissions, it simply asks the .NET
security system if the 'code' has read permissions for the file.
But one you call System.IO.FileStream filestr = new
FileStream(logpath,System.IO.FileMode.Open,System. IO.FileAccess.Read); windows will try to open the file for reading, but will throw if another
process(or another thread in this process) has the file open in exclusive
mode (non shared).

Willy.

"Vinayak Raghuvamshi" <vs************@yahoo.com> wrote in message
news:8d**************************@posting.google.c om...I have a file that has been created using shared read/write access.
But C# code never is able to open this file for read and keeps
throwing "...cannot open file as it is being used by another
process...".

I verified that the file is indeed available for shared reading. here
is my code snippet. is there a bug in C# that i dont know, or is there
a bug in my code?

FileIOPermission perm = new FileIOPermission(PermissionState.None);

perm.SetPathList(System.Security.Permissions.FileI OPermissionAccess.Read,
logpath);

perm.Demand( ); // can we do shared reading? this call succeeds

System.IO.FileStream filestr = new
FileStream(logpath,System.IO.FileMode.Open,System. IO.FileAccess.Read);
// still throws

any hints?

-Vinayak

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bill Burwell | last post by:
I am converting a VB6 WebClass application to VB.Net. Used the VB upgrade tool to do the conversion - and it left me a lot of little code things to do. Did those code things and got my app to...
5
by: Tim Eliot | last post by:
Just wondering if anyone has hit the following issue and how you might have sorted it out. I am using the command: DoCmd.TransferText acExportMerge, , stDataSource, stFileName, True after...
3
by: Joe Costa | last post by:
I have written the following code to search for the right file depending on the startup file for "Client Access". The menu database that I made will load the correct config file specific for each...
11
by: sur | last post by:
Hello, My problem is that File.Exists works fine if my file is on my local drive but returns false if its on any other drive. I think that the issue is probably file permissions and so I have...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
0
by: microb0x | last post by:
Is there any difference in the way an Access .mdb file is launched from directly double-clicking the file through windows explorer versus using code within another Access file to launch the...
9
by: JimmyKoolPantz | last post by:
IDE: Visual Studio 2005 Language: VB.NET Fox Pro Driver Version: 9.0.0.3504 Problem: I currently have a problem altering a DBF file. I do not get any syntax errors when running the program. ...
9
by: pob | last post by:
I currently have a procedure that loops thru a recordset to determine what files need to be loaded to my database. The naming convention of the files has always been accounts.txt, namelist.txt,...
12
by: dbuchanan | last post by:
Hello, (Is this the proper newsgroup?) === Background === I am building a solution with two projects. One project is my data access layer which contains my DataSet as an xsd file. The XSD...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.