Autor Beitrag
12profi
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 170

Win 7
C# (VS 2010, #Develop)
BeitragVerfasst: Mi 25.08.10 18:59 
Hallo,weiß einer von euch wieso dieser code nix bewirkt ????


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:
38:
39:
40:
41:
42:
43:
44:
45:
StreamWriter sw = new StreamWriter("Was sie gespeichert haben");
sw.Write(textBox4.Text, textBox5.Text, textBox6.Text, textBox7.Text, textBox8.Text, textBox9.Text, textBox10.Text, textBox11.Text, textBox12.Text, textBox13.Text, textBox14.Text, textBox15.Text, textBox19.Text, textBox18.Text, textBox20.Text, textBox21.Text, textBox22.Text, textBox24.Text, textBox25.Text, textBox26.Text, textBox27.Text, textBox28.Text, textBox29.Text, textBox30.Text, textBox35.Text, textBox34.Text, textBox36.Text, textBox37.Text);
sw.Close();



StreamReader sr = new StreamReader("Was sie gespeichert haben");
while (!sr.EndOfStream)
{

textBox4.Text += sr.ReadLine();
textBox5.Text += sr.ReadLine();
textBox6.Text += sr.ReadLine();
textBox7.Text += sr.ReadLine();
textBox8.Text += sr.ReadLine();
textBox9.Text += sr.ReadLine();
textBox10.Text += sr.ReadLine();
textBox11.Text += sr.ReadLine();
textBox12.Text += sr.ReadLine();
textBox13.Text += sr.ReadLine();
textBox14.Text += sr.ReadLine();
textBox15.Text += sr.ReadLine();
textBox19.Text += sr.ReadLine();
textBox18.Text += sr.ReadLine();
textBox20.Text += sr.ReadLine();
textBox21.Text += sr.ReadLine();
textBox22.Text += sr.ReadLine();
textBox24.Text += sr.ReadLine();
textBox25.Text += sr.ReadLine();
textBox26.Text += sr.ReadLine();
textBox27.Text += sr.ReadLine();
textBox28.Text += sr.ReadLine();
textBox29.Text += sr.ReadLine();
textBox30.Text += sr.ReadLine();
textBox35.Text += sr.ReadLine();
textBox34.Text += sr.ReadLine();
textBox36.Text += sr.ReadLine();
textBox37.Text += sr.ReadLine();
}
{
sr.Close();
}
}
}
}


Ich hoffe ihr könnt mir helfen

mfg 12profi

_________________
Mit was ich programmierer wollt ihr wissen ? Ich , ich programmiere mit c#


Zuletzt bearbeitet von 12profi am Mi 25.08.10 22:36, insgesamt 2-mal bearbeitet
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19326
Erhaltene Danke: 1749

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mi 25.08.10 19:34 
Meinst du im Ernst, dass "Was sie gespeichert haben" ein guter Dateiname ist? :roll:
Davon abgesehen fehlt die Angabe des Pfades zum Speichern...
12profi Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 170

Win 7
C# (VS 2010, #Develop)
BeitragVerfasst: Mi 25.08.10 19:38 
Aber ein Streamwritter braucht doch kein Pfad oder?

_________________
Mit was ich programmierer wollt ihr wissen ? Ich , ich programmiere mit c#
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19326
Erhaltene Danke: 1749

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Mi 25.08.10 20:07 
Wenn du in kein Verzeichnis und eine Datei darin schreiben willst, wohin denn dann? :shock:
12profi Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 170

Win 7
C# (VS 2010, #Develop)
BeitragVerfasst: Mi 25.08.10 20:56 
Kannst du mir bitte ein Beispiel geben

_________________
Mit was ich programmierer wollt ihr wissen ? Ich , ich programmiere mit c#
Yogu
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2598
Erhaltene Danke: 156

Ubuntu 13.04, Win 7
C# (VS 2013)
BeitragVerfasst: Mi 25.08.10 22:37 
user profile iconjaenicke hat folgendes geschrieben Zum zitierten Posting springen:
Wenn du in kein Verzeichnis und eine Datei darin schreiben willst, wohin denn dann? :shock:

Zum Beispiel in den RAM. Dafür muss an die StreamReader- und -Writer-Klassen ein Stream übergeben werden.

Also so:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
Stream = neuer Stream
Writer = neuer Writer (Stream)
Schreibe in Writer
Setzte Stream zurück
Reader = neuer Reader (Stream)
Lese aus Reader
12profi Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 170

Win 7
C# (VS 2010, #Develop)
BeitragVerfasst: Mi 25.08.10 22:57 
ok danke

_________________
Mit was ich programmierer wollt ihr wissen ? Ich , ich programmiere mit c#
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4799
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Do 26.08.10 10:46 
Hallo 12profi (oder doch eher 12halbprofi -)

bzgl. deiner Textboxen schau dir mal diesen Beitrag (und dessen Links an): Variablennamen zur Laufzeit zusammensetzen www.mycsharp.de/wbb2...d.php?threadid=21211
12profi Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 170

Win 7
C# (VS 2010, #Develop)
BeitragVerfasst: Do 26.08.10 13:35 
Hmmmm danke erst mal nur ich habe jetzt ein zweites problem das erste habe ich so gelöst:Ich habe für jeden eintrag den ich brauch einen eigenen streamwritter erstellt aber wenn ich es mit dem streamreader wieder aufrufen will,dann stürztst das Programm ab:Hier ist der Code
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:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
            StreamWriter sw = new StreamWriter("Was sie gespeichert haben");
            sw.Write(textBox4.Text);
            sw.Close();
            StreamWriter se = new StreamWriter("f");
            se.Write(textBox6.Text);
            se.Close();
            StreamWriter sb = new StreamWriter("b");
            sb.Write(textBox5.Text);
            sb.Close();
            StreamWriter sk = new StreamWriter("a");
            sk.Write(textBox7.Text);
            sk.Close();
            StreamWriter sh = new StreamWriter("g");
            sh.Write(textBox8.Text);
            sh.Close();
            StreamWriter st = new StreamWriter("k");
            st.Write(textBox9.Text);
            st.Close();
            StreamWriter sz = new StreamWriter("l");
            sz.Write(textBox10.Text);
            sz.Close();
            
 StreamReader sr = new StreamReader("Was sie gespeichert haben");
            while (!sr.EndOfStream)
            {
                textBox4.Text += Console.ReadLine();
            }
            {
                sr.Close();
            }
        
       
            StreamReader sh = new StreamReader("b");
            while (!sh.EndOfStream)
            {
                textBox5.Text += Console.ReadLine();
            }
            {
                sh.Close();
            }
        
            StreamReader sl = new StreamReader("f");
            while (!sl.EndOfStream)
            {
                textBox6.Text += Console.ReadLine();
            }
            {
                sl.Close();
            }
        
            StreamReader sb = new StreamReader("a");
            while (!sb.EndOfStream)
            {
                textBox7.Text += Console.ReadLine();
            }
            {
                sb.Close();
            }
        
            StreamReader sq = new StreamReader("g");
            while (!sq.EndOfStream)
            {
                textBox8.Text += Console.ReadLine();
            }
            {
                sq.Close();
            }
        
            StreamReader sm = new StreamReader("k");
            while (!sm.EndOfStream)
            {
                textBox9.Text += Console.ReadLine();
            }
            {
                sm.Close();
            }
        
            StreamReader sp = new StreamReader("l");
            while (!sp.EndOfStream)
            {
                textBox10.Text += Console.ReadLine();
            }
            {
                sp.Close();
            }
        }

_________________
Mit was ich programmierer wollt ihr wissen ? Ich , ich programmiere mit c#
ThoMa
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 46
Erhaltene Danke: 3



BeitragVerfasst: Do 26.08.10 14:16 
Hallo,

wie schon vorab geschrieben: Ein StreamReader und ein StreamWriter setzen immer auf einen Stream auf, dass der Konstruktor einen String entgegen nimmt, ist mir neu. - Müsste ich mal nach der konkreten Überladung gucken, die dahinter steckt.

Du müsstest Dich also entscheiden wo Du reinschreiben und aus was Du lesen möchtest.

Aus einem FileStream, also einer Datei?
Aus einem MemoryStream, also dem Arbeitsspeicher?
Aus einem NetworkStream, Netzwerk?

Dann erst den Stream instanzieren und dem Constructor des StreamWriters bzw. StreamReaders den Stream übergeben.

Grüße
ThoMa
Ccenter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 154

Win7
C#
BeitragVerfasst: Do 26.08.10 14:18 
Nach "new StreamWriter" musst du in Klammern den Pfad angeben, wohin das Dokument gespeichert werden soll, einschließlich dem Dateinamen + Endung. Also z.B. new StreamWriter(C:\Users\12profi\Desktop\test.txt)
Trashkid2000
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 561
Erhaltene Danke: 137



BeitragVerfasst: Do 26.08.10 14:20 
Hi,

was übergibst Du denn der Funktion sw.write()?

ausblenden C#-Quelltext
1:
sw.Write(textBox4.Text, textBox5.Text, textBox6.Text, textBox7.Text, textBox8.Text, textBox9.Text....)					


Hast Du Dir denn mal angeschaut, was dabei rauskommt?
Also, nach der Ausführung steht genau ein Wert in der erzeugten Datei, nämlich der von textBox4.Text!
Denn bei der Überladung ist das erste Argument der FormatString, und der Rest dann die Parameter für den FormatString.

Also, so wird das nichts. Auch wenn es so eine Überladung geben würde, wie Du sie hier gerne haben würdest (nämlich, dass alle Werte aneinandergepackt werden), so wäre dies auch sinnlos, wenn Du danach zeilenweise einliest.

Also müsstest Du schon sowas schreiben:
ausblenden C#-Quelltext
1:
2:
3:
sw.WriteLine(textBox1.Text);
sw.WriteLine(textBox2.Text);
...

Aber bei der Anzahl der TextBoxen auch nicht toll.

Und diese Abfrage auf Ende des Streams wie hier
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
while (!sr.EndOfStream)
{
  textBox4.Text += sr.ReadLine();
  textBox5.Text += sr.ReadLine();
  ...
}

ist auch schon ganz schön sinnlos, da Du nur am Anfang einmal prüfst, und dann geschätzte 100 Zeilen liest. Und was ist, wenn nach dem 3. Mal lesen schon das Ende erreicht wird? Dann wird lustig weitergelesen.

Was soll es denn mal werden, wenn es fertig ist? Sollen denn die Werte überhaupt in eine Datei geschrieben werden, oder wohin?
Nicht traurig sein, jeder fängst mal an!

Marko
Trashkid2000
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 561
Erhaltene Danke: 137



BeitragVerfasst: Do 26.08.10 14:24 
Was mir gerade aufgefallen ist:
ausblenden C#-Quelltext
1:
textBox4.Text += Console.ReadLine();					


Was ist das denn für eine Anwendung?

textBox4 ->Winforms? Wie willst Du denn da von der Console lesen?
Console.ReadLine() -> Konsolenanwendung? Wo hast Du denn da eine textBox reinbekommen?
Ccenter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 154

Win7
C#
BeitragVerfasst: Do 26.08.10 14:43 
@Trashkid2000, ich denke das würde klappen, wenn du eine Consolenanwendung erstellst und dann einfach eine Winform hinzufügst.
12profi Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 170

Win 7
C# (VS 2010, #Develop)
BeitragVerfasst: Do 26.08.10 17:07 
Ja mit Console war ein versehehen aber leute hallo einen streamwriter hat man doch immer oder fast immer so definiert
ausblenden C#-Quelltext
1:
2:
3:
streamwriter sw=new streamwriter("den namen")
sw.write(textBox1.Text);
sw.close();

schaut mal bei youtube unter c# datein lesen und schreiben

Moderiert von user profile iconChristian S.: C#-Tags hinzugefügt

_________________
Mit was ich programmierer wollt ihr wissen ? Ich , ich programmiere mit c#
Yogu
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2598
Erhaltene Danke: 156

Ubuntu 13.04, Win 7
C# (VS 2013)
BeitragVerfasst: Do 26.08.10 17:54 
user profile icon12profi hat folgendes geschrieben Zum zitierten Posting springen:
schaut mal bei youtube unter c# datein lesen und schreiben

Ok, das Schlüsselwort. Dateien lesen und schreiben. Also nichts mit MemoryStream oder NetworkStream, sondern ein FileStream.

user profile iconThoMa hat folgendes geschrieben Zum zitierten Posting springen:
Ein StreamReader und ein StreamWriter setzen immer auf einen Stream auf, dass der Konstruktor einen String entgegen nimmt, ist mir neu.

Wird ein String als Parameter übergeben, liest man aus, bzw. schreibt man in, eine Datei mit dem angegeben Dateinamen.

user profile icon12profi hat folgendes geschrieben Zum zitierten Posting springen:
Hmmmm danke erst mal nur ich habe jetzt ein zweites problem das erste habe ich so gelöst:Ich habe für jeden eintrag den ich brauch einen eigenen streamwritter erstellt

Hast du user profile iconTrashkid2000s ersten Beitrag schon gelesen?

user profile icon12profi hat folgendes geschrieben Zum zitierten Posting springen:
aber wenn ich es mit dem streamreader wieder aufrufen will,dann stürztst das Programm ab

Ich bezweilfe, dass das Programm einfach abstürzt, das tun .NET-Programme normalerweise nicht. Tritt eine Exception auf? Und wie lautet die Fehlermeldung?
Trashkid2000
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 561
Erhaltene Danke: 137



BeitragVerfasst: Do 26.08.10 18:27 
user profile icon12profi hat folgendes geschrieben Zum zitierten Posting springen:

schaut mal bei youtube unter c# datein lesen und schreiben


Aslo, dass man bei youtube nach programmiertechnischen Sachen sucht, habe ich auch noch nie gehört.
Ich denke, da gibt es tausend mal bessere Quellen im Netz (z.B. die MSDN).

Ja, wenn Du dem Streamwriter einen String übergibst, dann wird eine Datei erstellt. Und diese landet genau an dem Ort, wo das Programm hinkompiliert wird. Wohin auch, wenn kein Pfad angegeben wird. Und eine Dateiendung hat die Datei auch nicht, da ja keine angegeben wurde.

Soviel zu Videobeiträgen auf youtube.
ThoMa
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 46
Erhaltene Danke: 3



BeitragVerfasst: Fr 27.08.10 10:06 
Hallo,

ich möchte hier noch etwas hinzufügen:

Wenn man die OpenFile- und SaveFileDialoge verwendet ohne die Eigenschaft RestoreDirectory explizit auf 'true' zu setzen, ändert sich der Arbeitspfad der Anwendung. Dateien, die nicht explizit mit Pfad angegeben werden, werden so also in dem zuletzt gewählten Verzeichnis der Dialoge landen.

Grüße
ThoMa
12profi Threadstarter
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 170

Win 7
C# (VS 2010, #Develop)
BeitragVerfasst: Sa 28.08.10 09:18 
Ahaaa

_________________
Mit was ich programmierer wollt ihr wissen ? Ich , ich programmiere mit c#
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19326
Erhaltene Danke: 1749

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Sa 28.08.10 10:41 
Das gilt aber nicht nur für Dialoge. Das Arbeitsverzeichnis muss generell nicht mit dem Pfad der Anwendung übereinstimmen, es sei denn man setzt das explizit.

Und dann kann man auch gleich den Pfad bei den Dateioperationen angeben...