Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. try {} catch {} not working

try {} catch {} not working

Scheduled Pinned Locked Moved C#
question
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    ShadowUz
    wrote on last edited by
    #1

    Hello! I want to know if file is being used by another process and wait until it's free. I'm using the following code:

    public static bool FileInUse(string path
    {
    bool blnReturn = false;
    FileInfo file = new FileInfo(path);
    System.IO.FileStream fs;
    try
    {
    fs = System.IO.File.Open(file.FullName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None);
    fs.Close();
    }
    catch
    {
    blnReturn = true;
    }
    return blnReturn;
    }
    )

    ; But, instead of returning a value, the programm is crashing in

    fs = System.IO.File.Open(file.FullName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None);

    What is wrong here?

    B W E 3 Replies Last reply
    0
    • S ShadowUz

      Hello! I want to know if file is being used by another process and wait until it's free. I'm using the following code:

      public static bool FileInUse(string path
      {
      bool blnReturn = false;
      FileInfo file = new FileInfo(path);
      System.IO.FileStream fs;
      try
      {
      fs = System.IO.File.Open(file.FullName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None);
      fs.Close();
      }
      catch
      {
      blnReturn = true;
      }
      return blnReturn;
      }
      )

      ; But, instead of returning a value, the programm is crashing in

      fs = System.IO.File.Open(file.FullName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None);

      What is wrong here?

      B Offline
      B Offline
      BobJanova
      wrote on last edited by
      #2

      Are you running in a debugger? They will often notify you of exceptions even inside a catch. If you run this application on its own that should work, though it has a couple of issues (you will create the file if it doesn't exist, and you should be catching the specific exception you are looking for if you are just trying to find if the file is locked by another app).

      S 1 Reply Last reply
      0
      • S ShadowUz

        Hello! I want to know if file is being used by another process and wait until it's free. I'm using the following code:

        public static bool FileInUse(string path
        {
        bool blnReturn = false;
        FileInfo file = new FileInfo(path);
        System.IO.FileStream fs;
        try
        {
        fs = System.IO.File.Open(file.FullName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None);
        fs.Close();
        }
        catch
        {
        blnReturn = true;
        }
        return blnReturn;
        }
        )

        ; But, instead of returning a value, the programm is crashing in

        fs = System.IO.File.Open(file.FullName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None);

        What is wrong here?

        W Offline
        W Offline
        Wayne Gaylard
        wrote on last edited by
        #3

        I use this function,

        private static bool FileInUse(string path)
        {
        FileInfo file = new FileInfo(path);
        FileStream stream = null;
        try
        {
        stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
        }
        catch (IOException ex)
        {
        //MessageBox.Show(ex.Message);
        //File in use
        return true;
        }
        finally
        {
        if (stream != null)
        stream.Close();
        }
        return false;
        }

        although it will only return true if the file is Locked by another application(or yours). If the other application has opened the file without locking it will return false. You should be careful with your implementation as if the file is not found it would create one.

        ...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....

        S 1 Reply Last reply
        0
        • B BobJanova

          Are you running in a debugger? They will often notify you of exceptions even inside a catch. If you run this application on its own that should work, though it has a couple of issues (you will create the file if it doesn't exist, and you should be catching the specific exception you are looking for if you are just trying to find if the file is locked by another app).

          S Offline
          S Offline
          ShadowUz
          wrote on last edited by
          #4

          Thanks a lot! I runned the app by its own as you told, and that worked!

          modified on Thursday, April 21, 2011 5:38 AM

          B 1 Reply Last reply
          0
          • W Wayne Gaylard

            I use this function,

            private static bool FileInUse(string path)
            {
            FileInfo file = new FileInfo(path);
            FileStream stream = null;
            try
            {
            stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
            }
            catch (IOException ex)
            {
            //MessageBox.Show(ex.Message);
            //File in use
            return true;
            }
            finally
            {
            if (stream != null)
            stream.Close();
            }
            return false;
            }

            although it will only return true if the file is Locked by another application(or yours). If the other application has opened the file without locking it will return false. You should be careful with your implementation as if the file is not found it would create one.

            ...and I have extensive experience writing computer code, including OIC, BTW, BRB, IMHO, LMAO, ROFL, TTYL.....

            S Offline
            S Offline
            ShadowUz
            wrote on last edited by
            #5

            Thanks for the answer. I will change "FileMode.OpenOrCreate" to "FileMode.Open" as you said. The oly problem that was causing the program crash was I was running it in debug mode. I have runned it by its own as BobJanova advised, and now it is working fine.

            1 Reply Last reply
            0
            • S ShadowUz

              Thanks a lot! I runned the app by its own as you told, and that worked!

              modified on Thursday, April 21, 2011 5:38 AM

              B Offline
              B Offline
              BobJanova
              wrote on last edited by
              #6

              Welcome. You should be able to press Continue or similar (it's F5 in the debuggers I know) when you get an exception breakpoint inside a try when running in the debugger, by the way. You can also set your debugger not to notify you of caught exceptions.

              1 Reply Last reply
              0
              • S ShadowUz

                Hello! I want to know if file is being used by another process and wait until it's free. I'm using the following code:

                public static bool FileInUse(string path
                {
                bool blnReturn = false;
                FileInfo file = new FileInfo(path);
                System.IO.FileStream fs;
                try
                {
                fs = System.IO.File.Open(file.FullName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None);
                fs.Close();
                }
                catch
                {
                blnReturn = true;
                }
                return blnReturn;
                }
                )

                ; But, instead of returning a value, the programm is crashing in

                fs = System.IO.File.Open(file.FullName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None);

                What is wrong here?

                E Offline
                E Offline
                Ennis Ray Lynch Jr
                wrote on last edited by
                #7

                Unfortunately, your method and the method below does not work. If you place a lock on the file you have to wait for the O.S. to release the file. Thus a call FileInUse that returns true can still fail on the file open. The only way is to open the file and lock the file and return that lock.

                Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups