Forum » Programiranje » [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
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?
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?
- spremenil: DuleKrtola ()
DuleKrtola ::
OPEN
SAVE
EDIT: includat je treba tele (kšn je mogoče odveč...:)
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…
- spremenil: DuleKrtola ()
darkolord ::
Namesto tega
Lahko kar
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 ...
Tema | Ogledi | Zadnje sporočilo | |
---|---|---|---|
Tema | Ogledi | Zadnje sporočilo | |
» | [c#] iz class v classOddelek: Programiranje | 872 (746) | darkolord |
» | [c#] savefiledialog, en sfd a več datotekOddelek: Programiranje | 737 (665) | darkkk |
» | [c#] shrani text, tabele, slikeOddelek: Programiranje | 1502 (1108) | klemen93 |
» | C# text v .pdfOddelek: Programiranje | 1416 (1205) | japol |
» | [VB] OpenOddelek: Programiranje | 1522 (1361) | Monster |