Autor Beitrag
LL0rd
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Fr 10.08.07 12:18 
Hallo,

ich habe da folgendes Problem:

In meinem Programm werden Daten aus einer csv Datei eingelesen, um im Programm weiterbearbeitet zu werden.

ausblenden C#-Quelltext
1:
StreamReader sread = new StreamReader(new FileStream(NAME, FileMode.Open, FileAccess.Read));					


Die Datei selbst wird mit excel geöffnet und bearbeitet. Nun tritt aber ein Fehler auf, wenn die Datei mit Excel geöffnet wird:

ausblenden Quelltext
1:
The process cannot access the file '.....' because it is being used by another process					


Ja, ist klar, da die Datei ja auch in einem anderen Programm geöffnet ist. Aber ich will die Datei trotzdem öffnen und lesen. Der Notepad kann ja auch auf die Datei zugreifen, sie mit Excel geöffnet ist (schreiben geht nicht, will ich aber auch nicht).

Hat von euch jemand eine Idee, wie ich diesen Fehler verhindern kann?
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Fr 10.08.07 12:38 
Es gibt noch einen Konstruktor von FileStream, dem man als vierten Parameter einen FileShare-Wert übergeben kann. Wenn Du dort FileShare.Read übergibst, könnte es das tun.

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
LL0rd Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Fr 10.08.07 13:00 
Hi,

Danke für die Antwort. Du meintest es so, oder?

ausblenden C#-Quelltext
1:
StreamReader sread = new StreamReader(new FileStream(NAME, FileMode.Open, FileAccess.Read, FileShare.Read));					


Es kommt da immernoch der gleiche Fehler.
le Cauchemar
ontopic starontopic starontopic starhalf ontopic starofftopic starofftopic starofftopic starofftopic star
Beiträge: 24

Win XP
C#
BeitragVerfasst: Fr 10.08.07 13:30 
Versuch es mal mit
ausblenden C#-Quelltext
1:
StreamReader sread = new StreamReader(new FileStream(NAME, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));					

FileShare gibt doch an, wie die Datei von anderen Prozessen verwendet werden kann, oder?
LL0rd Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Fr 10.08.07 13:42 
Same....

Da kommt der gleiche Fehler raus. Wie öffnet denn z.B. Notepad die Datei? Macht es das nicht auf die gleiche Art?
LL0rd Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 19



BeitragVerfasst: Mo 13.08.07 11:24 
Hat denn keiner eine Idee?