473,324 Members | 2,254 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,324 software developers and data experts.

Reading an opened file in C#

I have a situation where I have an application, written by another party in
C++, that logs specific information to a log file. I am trying to open and
read this file in C# while the application is running.

The problem is I'm getting a sharing violation when attempting to open the
file. I recall that there was a flag or setting that could be used to open
files even when they're in use by another application, but I may be
mistaken.

Does anyone know of such a flag (I didn't see one in the documentation)? Or
does anyone know of a more preferred manner with which to accomplish this?

Thanks for any insight.

Tim
Jul 15 '08 #1
4 2963
On Tue, 15 Jul 2008 11:14:05 -0700, Tim Royal <ti*******@sierraonline.com>
wrote:
I have a situation where I have an application, written by another party
in
C++, that logs specific information to a log file. I am trying to open
and
read this file in C# while the application is running.

The problem is I'm getting a sharing violation when attempting to open
the
file. I recall that there was a flag or setting that could be used to
open
files even when they're in use by another application, but I may be
mistaken. [...]
It depends on how the file was opened by the other application. If it
opened the file with a sharing mode that would allow shared access, then
as long as you use a compatible sharing mode, you can also open the file.

A common default though is complete exclusive access to the file. It's
possible that there's no way for your application to open the file until
the other application is done with it.

See the FileShare enumeration for more details:
http://msdn.microsoft.com/en-us/libr...fileshare.aspx

Pete
Jul 15 '08 #2
On Jul 15, 2:14*pm, "Tim Royal" <tim.ro...@sierraonline.comwrote:
I have a situation where I have an application, written by another party in
C++, that logs specific information to a log file. I am trying to open and
read this file in C# while the application is running.

The problem is I'm getting a sharing violation when attempting to open the
file. I recall that there was a flag or setting that could be used to open
files even when they're in use by another application, but I may be
mistaken.

Does anyone know of such a flag (I didn't see one in the documentation)? Or
does anyone know of a more preferred manner with which to accomplish this?

Thanks for any insight.

Tim
Hi,

You can do it IF AND ONLY IF the other app opened the file with access
to be shared at least for read.
If it does then you can read it with shared reading access.

Take a look at the version of FileStream constructor that receives a
FileAcces, FileMode and FileSharing parameters
Jul 15 '08 #3
Thanks for the info, and the lead on the enumeration. I'll investigate hgow
they're opening the file from the application.

Tim
"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Tue, 15 Jul 2008 11:14:05 -0700, Tim Royal <ti*******@sierraonline.com>
wrote:
>I have a situation where I have an application, written by another party
in
C++, that logs specific information to a log file. I am trying to open
and
read this file in C# while the application is running.

The problem is I'm getting a sharing violation when attempting to open
the
file. I recall that there was a flag or setting that could be used to
open
files even when they're in use by another application, but I may be
mistaken. [...]

It depends on how the file was opened by the other application. If it
opened the file with a sharing mode that would allow shared access, then
as long as you use a compatible sharing mode, you can also open the file.

A common default though is complete exclusive access to the file. It's
possible that there's no way for your application to open the file until
the other application is done with it.

See the FileShare enumeration for more details:
http://msdn.microsoft.com/en-us/libr...fileshare.aspx

Pete

Jul 15 '08 #4
Maybe a daft idea, but what about making a copy of the file and then
manipulating the copied file. It might not be a practical solution,
just a stab in the dark.
Jul 16 '08 #5

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

Similar topics

2
by: opt_inf_env | last post by:
Hello, I would like to solve the following problem. On the server side I have a file with a sequence of natural numbers (1, 2, 3, 4, 5, ...., n). Each user, after some action, adds new number...
3
by: muser | last post by:
With the following code I'm trying to read a text file (infile) and output inaccuracies to the error file (printerfile). The text file is written and stored on disk, while the printerfile has to be...
10
by: pboulay | last post by:
Hi, I want to do like the command "tail -f" on unix. I want to read a LOG file and catch all new entries into that file. Someone have any idea? Thanks Patrick
0
by: Jim dunn | last post by:
HI I am having problems with C# with regards to its compatibility with win32 API methods, I am trying to read from a windows CE comm port using C# and imported methods from coredll.dll, it seems...
29
by: yourmycaffiene | last post by:
Okay, this if my first post so go easy on me plus I've only been using C for a couple of weeks. I'm working on a program currently that requires me to read data from a .dat file into a 2d array and...
2
by: GeoUK | last post by:
Hi All, New member here with a bit of problem. I have read the FAQ's and searched text books but still cannot solve the problem that I have. As part of a course I am doing at University I had...
10
by: lancer6238 | last post by:
Hi all, I'm having programs reading from files. I have a text file "files.txt" that contains the names of the files to be opened, i.e. the contents of files.txt are Homo_sapiens.fa...
3
by: rsk | last post by:
Hi Friends, I have the following code which reads the hexadecimal data from the "data.txt" file into the arrays. But when i run this code it is reading some garbage data after reading all the...
23
by: lisp9000 | last post by:
I wrote a small test program to read a file of data and print each line, but it's only printing the 2nd line out of 3 total lines. The test file, "foo.txt", has 3 lines: 7388: Zn->Z0 Run...
9
by: Amkcoder | last post by:
I am trying to read a file a character at a time, I want to read in the new line character. I am printing out each character to the screen. For some reason it will not print out the new line. I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.