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
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;
}
Re: Accessing file being used by another process
Quote:
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"
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
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.
Re: Accessing file being used by another process
Quote:
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!
Re: Accessing file being used by another process
Quote:
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 ]
:)