CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2011
    Posts
    5

    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

  2. #2
    Join Date
    May 2011
    Location
    Washington State
    Posts
    220

    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.

  3. #3
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    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.

  4. #4
    Join Date
    Jun 2011
    Posts
    5

    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

  5. #5
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    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.

  6. #6
    Join Date
    May 2011
    Location
    Washington State
    Posts
    220

    Re: Accessing file being used by another process

    Quote Originally Posted by BioPhysEngr View Post
    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!

  7. #7
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Accessing file being used by another process

    Quote Originally Posted by BioPhysEngr View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured