-
July 21st, 2011, 11:00 AM
#1
Accessing file being used by another process
Hi there,
I have a CSV file that is being written to by a sensor monitoring program.
I have my own program, which i want to read the data from the CSV file.
This all works, but only when the first program is not using the file.
How can i get access to the file so I can read the data at the same time as it's being written?
Thanks
Mike
-
July 21st, 2011, 12:08 PM
#2
Re: Accessing file being used by another process
What is the code you are using to read the file currently?
Please place code within (CODE)(/CODE) tags... but use [ ] rather than ( )...
Found a little method to exception-check a file on stackoverflow site... (http://stackoverflow.com/questions/8...file-is-in-use)
Code:
private bool IsFileLocked(FileInfo file)
{
FileStream stream = null;
try
{
stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None);
}
catch (IOException)
{
//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;
}
Last edited by fcronin; July 21st, 2011 at 12:14 PM.
-
July 21st, 2011, 10:47 PM
#3
Re: Accessing file being used by another process
Please place code within (CODE)(/CODE) tags... but use [ ] rather than ( )...
You can explain this more easily if you use [noparse] and [/noparse] tags to surround [code] and [/code] tags. So when you're editing the final thing looks like (in the editor window):
"Please use [noparse][code] and [/code][/noparse] tags around code"
and renders as:
"Please use [code] and [/code] tags around code"
Best Regards,
BioPhysEngr
http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
-
July 22nd, 2011, 03:41 AM
#4
Re: Accessing file being used by another process
Thanks for the replies.
I've got around it for now by having my program copy the file and then read that when it needs to.
Not ideal, but works for now.
cheers,
m
-
July 22nd, 2011, 05:36 AM
#5
Re: Accessing file being used by another process
Generally, the monitoring program must support it by not locking the file (it has to open it in shared mode). This OS/file system matter, so your options are limited. If it is possible, it would be better to use database as the store.
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post.
-
August 2nd, 2011, 07:09 PM
#6
Re: Accessing file being used by another process
Originally Posted by BioPhysEngr
You can explain this more easily if you use [noparse] and [/noparse] tags to surround [code] and [/code] tags. So when you're editing the final thing looks like (in the editor window):
"Please use [noparse][code] and [/code][/noparse] tags around code"
and renders as:
"Please use [code] and [/code] tags around code"
Good to know, thanks!
-
August 4th, 2011, 01:43 AM
#7
Re: Accessing file being used by another process
Originally Posted by BioPhysEngr
You can explain this more easily if you use [noparse] and [/noparse] tags to surround [code] and [/code] tags. So when you're editing the final thing looks like (in the editor window):
"Please use [noparse][code] and [/code][/noparse] tags around code"
and renders as:
"Please use [code] and [/code] tags around code"
Nice!! I've been around for ten years, and I've never even seen those tags! Thanks!
I always use the HTML ascii code for the square brackets, for example:
& #91; ( without the space ) gives you [ and & #93; ( without the space ) gives you ]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|