Recent

Author Topic: OpenDialog on Windows Vista "This file is in use"[SOLVED]  (Read 5395 times)

jolix

  • Jr. Member
  • **
  • Posts: 60
OpenDialog on Windows Vista "This file is in use"[SOLVED]
« on: December 11, 2010, 05:02:08 pm »
Hi,
How avoid Vista system Message Box "File already used" when atempt open a file opened by another process (i.e. file opened but not closed).
I have used OpenDialog and VistaOpenSaveDlg with same results.
On xp i can handle any errors, how do it on vista?
Code: [Select]
   if (VistaOpenSaveDialog(Handle, '', '', '', 'Open existing file', fileName,
        OFN_FILEMUSTEXIST, VDT_OPENDIALOG)) = True then begin
        ShowMessage(fileName);
        fn:=fileName;
        if IsFileInUse(fn) then begin
            ShowMessage('Can not open file');
        end
        else begin
            ShowMessage('File opened ok');
            AssignFile(ft, fn);
            Reset(ft, 1);
            FillListBox;  // do something with file contents
            CloseFile(ft);
            fOpen:=False;
        end;

    end
    else ShowMessage('Dialog Cancelled');

Thanks
Jolix
« Last Edit: December 14, 2010, 06:16:57 am by jolix »

ivan17

  • Full Member
  • ***
  • Posts: 173
Re: Windows Vista "File already used"
« Reply #1 on: December 11, 2010, 10:14:57 pm »
dialogs just return the file name (don't know about vista variety, though). they don't really open the file.

now stop using assign/reset

use TFileStream and specify file open mode as arguments to the constructor.

Code: [Select]
  try
    fs := TFileStream.Create(fn, fmOpenRead or fmShareDenyNone);
    <read the contents>
  finally
    fs.Free;
  end;

if you do it that way, it usually doesn't matter how many programs keep the file open.

by the way, in your case a simple call to ListBox.Items.LoadFromFile(fn) would probably work fine.

jolix

  • Jr. Member
  • **
  • Posts: 60
OpenDialog on Windows Vista "This file is in use"[SOLVED]
« Reply #2 on: December 12, 2010, 12:32:54 am »
Thanks for reply ivan17,

1. I know OpenDialog don't open file, at least on xp.
2. About your suggestion to use filestream instead, i will do it.
3. The example i place is a test, not a final work.

Note that my final work must have some sort of OpenDialog to choose the file with wich program will read every 30 seconds.
This file is left open 5 to 10 seconds every 2 minutes, by another app while append some info.

On xp, i can fire Dialog to just give me the file name i choose.
However on Vista, when i choose a file left open, Dialog stops with follow message:

Title:
  "Open existing file"
Message:
  "file aaa.txt"
  "This file is already in use"
  "Use a new filename or close the file opened by another program"

My questions is, how to avoid it? I just want filename with full path.

Thanks,
jolix
« Last Edit: December 14, 2010, 06:18:05 am by jolix »

jolix

  • Jr. Member
  • **
  • Posts: 60
OpenDialog on Windows Vista "This file is in use"[SOLVED]
« Reply #3 on: December 14, 2010, 06:14:09 am »
Hi again,

For share with people with same question, the solution is turn OpenDialog Options/ofShareAware=True.

This option can be found and modified on Object Inspector.

Thus, Windows vista Dialog no more stops with warning message box, when file selected is left open and let as handle this as we want.

url where this question is discussed:
http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/56fbbf9b-31d5-4e89-be85-83d9cb1d538c/

thanks,
jolix
« Last Edit: December 14, 2010, 06:18:28 am by jolix »

 

TinyPortal © 2005-2018