![]() |
Überprüfen ob Datei verfügbar
Ich habe ein Programm geschrieben, welches Daten in eine .csv Datei schreibt. Nun will ich dass das Programm schaut, ob die Datei verfügbar ist oder nicht (zbsp: Wenn noch geöffnet).
In php würde ich das so machen:
Delphi-Quellcode:
Aber das scheint in Delphi nicht möglich zu sein. Wisst ihr, was da abhilfe schafft?
if(Rewrite(f)) then ..{ablauf}..;
|
Re: Überprüfen ob Datei verfügbar
Probiers mal damit.
Delphi-Quellcode:
Wenn die Datei offen ist, exisitiert sie ja logischerweise auch.
FileExists(pfad)
ansonsten:
Delphi-Quellcode:
mittels {$I-} werden Fehlermeldungen unterdrückt und deren Nummer kann statt dessen mit IOResult abgefragt werden (nur einmal, deswegen in anderer Vari speichern, wenn damit gearbeitet werden soll!).
function dateiVorhanden(pfad:string):boolean;
var datei:file; begin assignFile(datei,pfad); filemode:=0; {$I-} reset(datei); closefile(datei); {$I+} dateiVorhanden:=IOResult=0 end; |
Re: Überprüfen ob Datei verfügbar
hmm....ob sie vorhanden ist, ist ja nicht die frage. Sondern eher, ob sie noch von excell geöffnet ist.
Dein 2tes Beispiel werd ich mal ausprobieren. |
Re: Überprüfen ob Datei verfügbar
Habs grad mal ausprobiert.. reset liefert auch 0, wenn die Datei geöffnet ist. da müssteste dann rewrite benutzen (und Filemode nicht auf 0 setzen). Das jedoch initialisiert die Datei.
Hab das vor kurzem in nem anderen Thread gesehen, da gabs ne API-Funktion, direkt zu checken, ob die Datei von einem Programm geöffnet ist. |
Re: Überprüfen ob Datei verfügbar
die api-funktion kenn ich nicht, aber du kannst mit
TFileStream.Create(Filename, fmShareExclusive) die Datei öffnen. Wenn die Datei schon geöffnet ist müsste eine exception ausgelöst werden |
Re: Überprüfen ob Datei verfügbar
Ich glaub das war's auch, wenn ich mir diesesm fmShareExclusive angucke. :gruebel:
|
Re: Überprüfen ob Datei verfügbar
Ich bin leider ein totaler neuling und weis nicht, was dieses "fmShareExklusive" ist.
|
Re: Überprüfen ob Datei verfügbar
Nur n Konstantenbezeichner. Ich meinte damit eben nur, dass mir der Name davon bekannt vorkommt.
Edit: Juhu! 20. Nachricht |
Re: Überprüfen ob Datei verfügbar
einfach mal in der OH zu TFileStream gucken, da sind die ganzen Share-Dinger erklärt
|
Re: Überprüfen ob Datei verfügbar
:( Bei mir steht da irgendwie in der OH nix zu dem fmShare dings.
|
Re: Überprüfen ob Datei verfügbar
To create or open a file and get access to a handle for the file, you simply instantiate a TFileStream. This opens or creates a named file and provides methods to read from or write to it. If the file cannot be opened, TFileStream raises an exception.
constructor Create(const filename: string; Mode: Word); The Mode parameter specifies how the file should be opened when creating the file stream. The Mode parameter consists of an open mode and a share mode or’ed together. The open mode must be one of the following values: Table 1.11 Open modes Value Meaning fmCreate TFileStream a file with the given name. If a file with the given name exists, open the file in write mode. fmOpenRead Open the file for reading only. fmOpenWrite Open the file for writing only. Writing to the file completely replaces the current contents. fmOpenReadWrite Open the file to modify the current contents rather than replace them. The share mode can be one of the following values with the restrictions listed below: Table 1.12 Share modes Value Meaning fmShareCompat Sharing is compatible with the way FCBs are opened. fmShareExclusive Other applications can not open the file for any reason. fmShareDenyWrite Other applications can open the file for reading but not for writing. fmShareDenyRead Other applications can open the file for writing but not for reading. fmShareDenyNone No attempt is made to prevent other applications from reading from or writing to the file. Note that which share mode you can use depends on which open mode you used. The following table shows shared modes that are available for each open mode. Open Mode fmShareCompat fmShareExclusive fmShareDenyWrite fmShareDenyRead fmShareDenyNone fmOpenRead Can’t use Can’t use Available Can’t use Available fmOpenWrite Available Available Can’t use Available Available fmOpenReadWrite Available Available Available Available Available The file open and share mode constants are defined in the SysUtils unit. |
Re: Überprüfen ob Datei verfügbar
Ich stell mich sicher zu blöd an. Denn es will net. Der Compiler meckert:
[Error] Unit1.pas(299): ')' expected but ',' found |
Re: Überprüfen ob Datei verfügbar
da hast du wohl irgendwas vergessen. sollte ungefähr so aussehen
Delphi-Quellcode:
Edit: hatte vergessen "delphicode-tag" zu schließen
var mystream: TStream; //oder TFileStream
begin try mystream := TFilestream.create('c:\meinedatei.txt', fmOpenReadWrite or fmShareExclusive); //lesen schreiben etc finally mystream.free; end; end; |
Re: Überprüfen ob Datei verfügbar
Jo danke...es klappt :)
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 02:08 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz