Autor Beitrag
Jacdelad
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 109

Win XP Pro
Borland Developer Studio 2006 Architect
BeitragVerfasst: Sa 13.05.06 13:01 
Hallo,

gibt es eine Funktion mit der man ermitteln kann, ob eine Datei (Variable vom Typ file oder textfile) geöffnet ist?

Jac


Moderiert von user profile iconChristian S.: Topic aus Delphi Language (Object-Pascal) / CLX verschoben am So 14.05.2006 um 11:49

_________________
Es gibt 10 Arten von Menschen: Die einen, die Binärcode verstehen und die, die es nicht tun.
azubi_20
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 593

WinXP SP2, Ubuntu 8.4
D7 Enterp., D2005 Prof., Java (Eclipse 3.4.0)
BeitragVerfasst: Mo 15.05.06 08:57 
Hi
du könntest versuchen, auf die Datei zuzugreifen. Mit nem Try-Exceptblock kannst du dann prüfen ob es geklappt hat. Wenn nicht -> Datei ist geöffnet. (könnte aber auch schreibgeeschützt sein)
raiguen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 374

WIN 2000prof, WIN XP prof
D7EP, MSSQL, ABSDB
BeitragVerfasst: Mo 15.05.06 09:16 
Moin :-)
Schau mal diesen Beitrag
DaKirsche
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 187

Win XP Pro, SuSe Linux 7.3 - 10.2, Win 2k3 Server, Win 2000, Win NT 4.0
Delphi 2006 Pro, Java, HTML, SQL, PHP, CSS
BeitragVerfasst: Mo 15.05.06 09:22 
ausblenden Delphi-Quelltext
1:
uses FileCtrl;					

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
function IsFileInUse(Path: string): boolean;
var
  hFile: THandle;

begin
  Result := False;
  if not FileExists(Path) then Exit;

  hFile := CreateFile(pchar(Path), GENERIC_READ or GENERIC_WRITE or GENERIC_EXECUTE,
                      0nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  Result := hFile = INVALID_HANDLE_VALUE;
  if not Result then CloseHandle(hFile);

end{function IsFileInUse() ...}

Aufgerufen werden kann die Funktion Beispielsweise so:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
procedure TForm1.Button1Click(Sender: TObject);
begin
  if IsFileInUse(Application.Exename) then
    Showmessage('Die Datei ist in Benutzung');
end;
Jacdelad Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 109

Win XP Pro
Borland Developer Studio 2006 Architect
BeitragVerfasst: Mo 15.05.06 17:27 
Ähh, danke für die Hilfe!

Ich meinte eine Datei, die unter Umständen von meinem Programm geöffnet ist (sorry, das hab ich leider vergessen zu erwähnen), deshalb hab ichs jetzt über ein Boolean gelöst, das erscheint mir am einfachsten.

Danke,
Jac

_________________
Es gibt 10 Arten von Menschen: Die einen, die Binärcode verstehen und die, die es nicht tun.
IceFire
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 21



BeitragVerfasst: Mo 22.05.06 15:10 
Hallo!

Habe die Funtion gerade eingebaut, allerdings habe ich damit ein Problem. Wenn ich damit versuche, auf freigegebene Dateien im Netzwerk ('\\196.0.0.8\II\readme.txt') zuzugreifen, meldet mir die Funktion immer, dass die Datei geöffnet ist.

Wie ist die Funktion zu modifizieren, damit Netzwerkressourcen auch geprüft werden können?