File Access

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dan

    #1

    File Access

    All,

    I need to know if anyone knows if there is a way to tell
    if a file is currently in use by some process. I already
    know you can wrap your file access call in a seperate try
    block but I would think that there is a more direct way to
    do that.

    Ideally it would be part of the Framework but if there is
    at least a direct API call I can make I would be happy
    with that. Any information would be great. Thanks!
  • Dan DeLuca

    #2
    Re: File Access

    Thanks for the response. What you suggested is actually
    what I am currently doing. While it works I would think
    that there has to be a more direct way to get at that kind
    of information. Obviously Windows must know what state a
    file is in otherwise the System.IO.IOExc eption could not
    be raised when you try to access a file. I was hoping that
    someone knows of a more direct way to get at that other
    than the try/catch technique. I would think that there
    would have to be some API call that could be done.

    [color=blue]
    >-----Original Message-----
    >That is a good question and I don't know the correct[/color]
    answer. But since no[color=blue]
    >one else has responded yet, here is one way that should[/color]
    work.[color=blue]
    >
    >Simply try to open the file to write to and trap an[/color]
    error. Like this:[color=blue]
    >
    >bool FileLocked = false;
    >try {System.IO.File .OpenWrite("MyF ile.exe");}
    >catch (System.IO.IOEx ception) {FileLocked = true;}
    >
    >
    >-Noah Coad
    > Microsoft MVP & MCP [.NET/C#]
    >
    >"Dan" <ddeluca@fnisol utions.com> wrote in message
    >news:030001c35 d2e$524d1cd0$a6 01280a@phx.gbl. ..[color=green]
    >> All,
    >>
    >> I need to know if anyone knows if there is a way to tell
    >> if a file is currently in use by some process. I already
    >> know you can wrap your file access call in a seperate[/color][/color]
    try[color=blue][color=green]
    >> block but I would think that there is a more direct way[/color][/color]
    to[color=blue][color=green]
    >> do that.
    >>
    >> Ideally it would be part of the Framework but if there[/color][/color]
    is[color=blue][color=green]
    >> at least a direct API call I can make I would be happy
    >> with that. Any information would be great. Thanks![/color]
    >
    >
    >.
    >[/color]

    Comment

    Working...