» »

[C#] textbox to .txt file

[C#] textbox to .txt file

DuleKrtola ::

Tale koda ob kliku na gumb shrani tekst iz RichTextBox v .rtf file. Mene zanima koda, ki shrani text iz navadnega textboxa (NE RICH TEXT) v .txt file

        private void button2_Click(object sender, EventArgs e)
        {
            SaveFileDialog SaveDialog = new SaveFileDialog();
            SaveDialog.Title = "Save";

            try
            {
                if (SaveDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    richTextBox1.SaveFile(SaveDialog.FileName);
                }
            }
            catch (Exception ex)
            {
            }
        }

Mene zanima koda, ki shrani text iz navadnega textboxa (NE RICH TEXT) v .txt file, in koda za odpret file. Se pravi, kako bi zgledala ukaza

richTextBox1.LoadFile(OpenDialog.FileName);
richTextBox1.SaveFile(SaveDialog.FileName);

za TextBox1?

DuleKrtola ::

Rešeno ... Mod lah kar zbriše temo

l0g1t3ch ::

Lahko prilepis resitev za naslednje generacije ;)

DuleKrtola ::

OPEN
private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog OpenDialog = new OpenFileDialog();
            OpenDialog.Title = "Open";
            OpenDialog.Filter = "All Files|*.*";
            try
            {
                if (OpenDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    System.IO.StreamReader objReader;
                    objReader = new System.IO.StreamReader(OpenDialog.FileName);
                    textBox.Text = objReader.ReadToEnd();
                    objReader.Close();
                }
            }
            catch (ArgumentException ex)
            {
                MessageBox.Show("Invalid File Format");
            }
            catch (Exception ex)
            { }
        }


SAVE
private void button2_Click(object sender, EventArgs e)
        {
            SaveFileDialog SaveDialog = new SaveFileDialog();
            SaveDialog.Title = "Save";

            try
            {
                if (SaveDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    StreamWriter sw = new StreamWriter(SaveDialog.FileName);
                    String[] contents = textBox.Lines.ToArray();
                    for (int i = 0; i < contents.Length; i++)
                    {
                        sw.WriteLine(contents[i]);
                    }
                    sw.Close();
                }
            }
            catch (Exception ex)
            {
            }
        }



EDIT: includat je treba tele (kšn je mogoče odveč...:)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

Zgodovina sprememb…

darkolord ::

Namesto tega

StreamWriter sw = new StreamWriter(SaveDialog.FileName); 
String[] contents = textBox.Lines.ToArray(); 
for (int i = 0; i < contents.Length; i++) 
{ 
     sw.WriteLine(contents[i]); 
} 
sw.Close();


Lahko kar

StreamWriter sw = new StreamWriter(SaveDialog.FileName); 
sw.Write(textBox.Text);
sw.Close();


Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

[c#] iz class v class

Oddelek: Programiranje
5872 (746) darkolord
»

[c#] savefiledialog, en sfd a več datotek

Oddelek: Programiranje
8737 (665) darkkk
»

[c#] shrani text, tabele, slike

Oddelek: Programiranje
251502 (1108) klemen93
»

C# text v .pdf

Oddelek: Programiranje
81416 (1205) japol
»

[VB] Open

Oddelek: Programiranje
71522 (1361) Monster

Več podobnih tem