Answered How to check if a .txt file is allready open

  • Thursday, March 15, 2007 10:14 PM
     
     
    I have a .txt file which I want to open, but I only want to open just one instance.  How do I check if the file is all ready open?

All Replies

  • Thursday, March 15, 2007 11:52 PM
     
     
    Do this..

    try
    {
        File.Open(Path, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
        // This means the file is not open yet.
    }
    catch
    {
         // this means the file is open already...
    }

    theTroll
  • Friday, March 16, 2007 1:34 AM
     
     
    You should catch the exception in particular the IOException, that's means that the file is alredy open or something like this.

    Here you've all the returned exceptions by File.Open

    Exception Condition
    ArgumentException path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.-or- access specified Read and mode specified Create, CreateNew, Truncate, or Append.
    ArgumentNullException path is null.
    DirectoryNotFoundException The specified path is invalid, (for example, it is on an unmapped drive).
    ArgumentOutOfRangeException mode, access, or share specified an invalid value.
    UnauthorizedAccessException path specified a file that is read-only and access is not Read.-or- path specified a directory.-or- The caller does not have the required permission.
    NotSupportedException path is in an invalid format.
    PathTooLongException The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
    IOException An I/O error occurred while opening the file.
    FileNotFoundException The file specified in path was not found.

    hope it helps.
  • Saturday, March 17, 2007 3:48 PM
     
     
    theTroll

    try
    {
        File.Open(Path, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
        // This means the file is not open yet.
    }
    catch
    {
         // this means the file is open already...
    }

    I have tried this out. '
    File.Open(Path, FileMode.Open, FileAccess.ReadWrite, FileShare.None);' This process runs, but when I tried to open the file through File.Open or System.Diagnostic.Process.Start, it says 'The process cannot access the file because it is being used by another process'.

    Any ideas of getting around the problem?

  • Saturday, March 17, 2007 4:17 PM
     
     
    The code that you tried should works, when it tries to do the File.Open an IOException should throw and pass through the catch statement:

    catch(IOException exception)
    {
        Console.WriteLine("Unable the open the file, it's already open.");
    }

    Do you mean that does not happen?

  • Saturday, March 17, 2007 4:24 PM
     
     
    That does happen but the opened file does not appear?
  • Saturday, March 17, 2007 6:22 PM
     
     
    The thing is that you can't open a file that is already open by another process, so if you getting the exception the file does not open.
  • Sunday, March 18, 2007 1:32 AM
     
     
    You can create another dummy hidden file when you open your txt file, with the same name but with different extension like MyFile.txt, ~MyFile.tmp so before you call your open method you check for the ~MyFile.tmp if it exists, and when you find it, so it means that it is already opened.
    and delete this file when you close your application or when you finish working with the file.
  • Monday, March 19, 2007 10:43 AM
     
     Answered

    Hi,

    There may be few other solutions, but i would like to give you the following one,

    Hope it will work for you,

            [DllImport("user32.dll")]

            private static extern int SetForegroundWindow(IntPtr hWnd);

     

            private void OpenFile(string sAppName4TXT, string sPath, string sFileName)

            {

                IntPtr ipProcHandle = IntPtr.Zero;

                Process[] procs = Process.GetProcessesByName(sAppName4TXT);

                foreach (Process proc in procs)

                {

                    if(proc.MainWindowTitle.Contains(sFileName))

                    {

                        ipProcHandle = proc.MainWindowHandle;

                        break;

                    }

                }

                if (ipProcHandle != IntPtr.Zero)

                {

                    //found proc

                    SetForegroundWindow(ipProcHandle);

                }

                else

                {

                    //not found - start new process for that file

                    Process myproc = new Process();

                    myproc.StartInfo.FileName = sPath + "\\" + sFileName;

                    myproc.Start();

                    SetForegroundWindow(myproc.MainWindowHandle);

                }

            }

    Calling code:

    OpenFile("notepad", @"C:\Test", "Test.txt");

     

    HTH,