Autor Beitrag
Lihlu
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 53



BeitragVerfasst: Do 24.05.12 14:29 
Hi,

Ich möchte gerne den Text aus meiner Textbox in eine txt Datei speichern.Bis jetzt habe ich nirgends etwas ganz einfaches gefunden :/

Wie genau stell ich das ganz EINFACH an ? :)

MFG
Lihlu


Moderiert von user profile iconTh69: Topic aus C# - Die Sprache verschoben am Do 24.05.2012 um 15:45
Lihlu Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 53



BeitragVerfasst: Do 24.05.12 14:35 
Hab es doch hinbekommen ...

Trotzdem vielen dank (:

Lösung:
ausblenden C#-Quelltext
1:
TextBox1.SaveFile("moep.txt");					


Moderiert von user profile iconTh69: Full-Quote entfernt
Moderiert von user profile iconTh69: C#-Tags hinzugefügt
Palladin007
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1282
Erhaltene Danke: 182

Windows 11 x64 Pro
C# (Visual Studio Preview)
BeitragVerfasst: Mo 28.05.12 22:15 
Hei, die Methode kannte ich ja noch gar nicht :D

Hätte es über einen FileStream gemacht, aber das ist nicht einfach, weil die nur bytes annimmt^^
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Mo 28.05.12 22:55 
Zitat:
Hei, die Methode kannte ich ja noch gar nicht


Kein Wunder die gibt es auch nicht. Eine RichTextbox könnte das.
Palladin007
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1282
Erhaltene Danke: 182

Windows 11 x64 Pro
C# (Visual Studio Preview)
BeitragVerfasst: Di 29.05.12 18:32 
Das gibts nicht? :(

Habs noch nicht ausprobiert, weil ich es noch nicht gebraucht hab -.-


Schade, fand die Idee gut^^

Kann ja ne Erweiterungsmethode schreiben
Palladin007
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1282
Erhaltene Danke: 182

Windows 11 x64 Pro
C# (Visual Studio Preview)
BeitragVerfasst: Di 29.05.12 18:52 
So, hier die Erweiterungsmethode:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
    public static class AddToTextBox
    {
        /// <summary>
        /// Speichert den Inhalt der System.Windows.Forms.TextBox in eine Datei (txt Format).
        /// </summary>
        /// <param name="path">Der Name und Speicherort der zu speichernden Datei.</param>
        /// <param name="Override">Wenn eine bereits vorhandene Datei überschrieben werden soll, true, andernfals false.</param>
        public static void SaveFile(this System.Windows.Forms.TextBox Box, string path, bool Override)
        {
            System.IO.FileMode mode;
            if(Override)mode=System.IO.FileMode.Create;
            else mode=System.IO.FileMode.CreateNew;
            System.IO.FileStream file = new System.IO.FileStream(path, mode, System.IO.FileAccess.Write);
            byte[] arr=new System.Text.ASCIIEncoding().GetBytes(Box.Text);
            file.Write(arr, 0, arr.Length);
            file.Close();
        }

    }


Wichtig ist bloß, dass die Methode in einer statischen Klasse liegt, sonst funktioniert es nicht.
Hab nicht getestet, ob sie auch in der Praxis funktioniert, sollte sie aber schon, zumindest wüsste ich nicht, was ich falsch gemacht haben sollte.


Zuletzt bearbeitet von Palladin007 am Di 29.05.12 20:21, insgesamt 1-mal bearbeitet

Für diesen Beitrag haben gedankt: Lihlu
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: Di 29.05.12 20:04 
Willst du eine sichere (safe) Datei oder aber eine Datei speichern (save)? ;-)

P.S. Mit File.WriteAllText(path, textBox.Text); geht das aber viel einfacher (und auch ohne dafür extra eine Erweiterungsmethode schreiben zu müssen).
Palladin007
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1282
Erhaltene Danke: 182

Windows 11 x64 Pro
C# (Visual Studio Preview)
BeitragVerfasst: Di 29.05.12 20:21 
Stimmt :D

Mensch, es gibt so viele Varianten, eine Datei zu schreiben, da verliert man mal fix den Überblick^^
Naja, gut, ich kenne mit der von dir genannten jetzt drei Möglichkeiten ^^


Und ja, hast Recht, für sowas braucht man keine Erweiterungsmethode.
Ist dann doch etwas zu unpraktisch, das Prinzip der OOP so auf zuweichen.


Ich lass sie trotzdem stehen, vielleicht findet es ja jemand praktisch ^^



Und den Namen ändere ich ganz schnell xD