Autor Beitrag
mr tobo
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 40



BeitragVerfasst: Do 05.02.09 22:02 
hallo.
Braucht das irgendwie etwas Zeit nach dem erstellen einer Datei?
Der Code:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
            File.Create(pfad);
            if (!fi.Exists)
                fi.Create();
            using (FileStream fs = fi.OpenWrite())
            {...}


kann zwar ohne Fehlermeldung kompiliert werden, bei ausführen des programmes tritt allerdings die
IOException ein:
Zitat:

Der Prozess kann nicht auf die Datei F:\Angriffe.btr zugreifen, da sie von einem anderen Prozess verwendet wird.


oder woran könnte das sonst liegen?
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Do 05.02.09 22:05 
Der Quelltext ist irgendwie total konfus (und wahrscheinlich nicht Dein Original-Quelltext?).

Die Fehlermeldung gibt doch aber einen deutlichen Hinweis, dass die Datei schon erstellt wurde und dass das also nicht der Fehler ist. Es sieht eher so aus, als wäre die Datei noch durch Deinen Prozess geöffnet.

Poste mal den richtigen Quelltext, dann kann man da mehr sagen.

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



BeitragVerfasst: Do 05.02.09 22:16 
ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
Byte[] info;
        static string pfad = @"F:\Angriffe.btr";
        FileInfo fi = new FileInfo(pfad);
        List<TextBox> tblist = new List<TextBox>();
        public Form_config()
        {
            InitializeComponent();
        }

        private void koords_save()
        {
            File.Delete(pfad);
            File.Create(pfad);
            if (!fi.Exists)
                fi.Create();
            using (FileStream fs = fi.OpenWrite())
            {
                int TB_Cursor = 0;
                foreach(TextBox TB in tblist)
                {   
                    TB_Cursor++;
                    switch (TB_Cursor)
                    {
                        case 15:
                            TB_Cursor = 0;
                            info =
                            new UTF8Encoding(true).GetBytes("" + TB.Text + Environment.NewLine);
                            break;
                        default
                            info =
                            new UTF8Encoding(true).GetBytes("" + TB.Text + ";");
                            break;
                    }
                    fs.Write(info, 0, info.Length);
                }
            }
        }


ich dachte einfach, der rest ist nicht von belang...
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Do 05.02.09 22:24 
Also, zuerst einmal erstellst Du die Datei mit
ausblenden C#-Quelltext
1:
File.Create(pfad);					

Das liefert Dir einen FileStream zu dieser Datei.

Dann prüfst Du nochmal ob die Datei erstellt ist und falls nicht, erstellst Du sie. Wie das nach obigem Befehl sein kann, weiß ihc nicht ;-)
ausblenden C#-Quelltext
1:
2:
if (!fi.Exists)
    fi.Create();


Dann willst Du einen FileStream zu der Datei erstellen, aber der obige Filestream ist ja noch offen. Da dürfte es also krachen:
ausblenden C#-Quelltext
1:
using (FileStream fs = fi.OpenWrite())					


Ach ja, das File.Delete braucht es eigentlich auch nicht, weil die Doku folgendes sagt:
Zitat:
File..::.Create Method (String)

Creates or overwrites a file in the specified path.


Die ersten fünf Zeilen sollten also eigentlich durch folgendes ersetzbar sein:
ausblenden C#-Quelltext
1:
using (FileStream fs = File.Create(pfad))					

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



BeitragVerfasst: Do 05.02.09 22:28 
geht.

danke

bin halt noch etwas unerfahren in diesem bereih. :oops: