2017-09-01 252 views
-1

我想用一个文件帮助文件将一个文本帮助文件保存为一个txt文件,所以我可以在我的程序开始时重新加载到richtextbox。C#将richtextbox的内容保存到一个文件

因此,如果我打印内容到一个字符串,然后我想将它转换为一个bytearray。在这之后,我想将bytearray打印到我的txt文件中。 而我想在我的程序开始时加载到richtextbox中的文本文件。

我filehelper:

namespace Kontomanager_0._3 
{ 
    public class filehelper 
    { 

    public filehelper(string myPath) 
    { 
     myPath = MyPath; 
    } 

    private string MyPath { get; } 

    public byte[] ReadByteString() 
    { 
     return File.ReadAllBytes(MyPath); 
    } 

    public void WriteAllBytes(byte[] arrayToWrite) 
    { 
     File.WriteAllBytes(MyPath, arrayToWrite); 
    } 

    internal static byte[] ReadAllBytesStatic(string myPath) 
    { 
     return File.ReadAllBytes(myPath); 
    } 


    } 
} 

我的Windows窗体代码:

namespace Kontomanager_0._3 
{ 
    //private filehelper FileHelper { get; set; } 
    public partial class Form1 : Form 
    { 
    filehelper file = new filehelper("Aktivitaeten.txt"); 

    //Gutschriftvariablen 
    string gBetrag; 
    string gAbsender; 
    string gDatum; 

    //Abbuchungvariablen 
    string aBetrag; 
    string aEmpfaenger; 
    string aDatum; 

    // Transaktionenzähler variablen 
    int counter; 
    string lbcounter; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void btnGutschrift_Click(object sender, EventArgs e) 
    { 
     Gutschrift(); 
    } 

    private void btnAbbuchung_Click(object sender, EventArgs e) 
    { 
     Abbuchung(); 
    } 

    private void btnSave_Click(object sender, EventArgs e) 
    { 
     Speichern(); 
    } 

    private void btnBeenden_Click(object sender, EventArgs e) 
    { 
     Close(); 
    } 


    void Gutschrift() 
    { 
     Zähler(); 
     gBetrag = this.tbgBetrag.Text; 
     gAbsender = this.tbgAbsender.Text; 
     gDatum = this.tbgDatum.Text; 

     this.tbLog.Text += "(" + lbcounter + ")" + "[" + gDatum + "] " + gBetrag + "€ Erhalten von" + gAbsender + "\n"; 
    } 

    void Abbuchung() 
    { 
     Zähler(); 
     aBetrag = this.tbaBetrag.Text; 
     aEmpfaenger = this.tbaEmpfaenger.Text; 
     aDatum = this.tbaDatum.Text; 

     this.tbLog.Text += "(" + lbcounter + ")" + "[" + aDatum + "] " + aBetrag + "€ Gesendet an: " + aEmpfaenger + "\n"; 
    } 

    void Zähler() 
    { 
     counter += 1; 

     lbcounter = counter.ToString(); 

     lbTransaktionen.Text = "Transaktionen: " + lbcounter; 
    } 

    void Speichern() 
    { 
     string text; 
     text = this.tbLog.Text; 
     string path = "Aktivitaeten.txt"; 

      DialogResult Result = MessageBox.Show("Datei hier abspeichern" + path, "Error", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information); 

      if(Result == DialogResult.Yes) 
      { 

       // File überschreiben und speichern 
       byte[] array = Encoding.UTF8.GetBytes(text); 
      File.WriteAllBytes(path, array); 

      } 
      else if (Result == DialogResult.No) 
      { 
       // 
      } 
    } 
    } 
} 
+0

你忘了描述你遇到的任何问题的部分... – David

+0

你准确的问题是什么?这段代码没有做你想做的事吗? – stuartd

+0

只是炫耀你已经有你要的代码?凉。 –

回答

1

这段代码是完全错误的

public filehelper(string myPath) 
{ 
    myPath = MyPath; 
} 

private string MyPath { get; } 

我想你想写

public filehelper(string myPath) 
{ 
    MyPath = myPath; 
} 

private string MyPath { get; set;} 

确定您使用的代码导致了MyPath的空值,您用来保存要读取和写入的文件的名称的变量。除此之外,你的代码似乎对我有用。

+1

不错的地方。 'File.WriteAllBytes' [如果传递给它的路径为空,则会抛出'ArgumentException'](https://msdn.microsoft.com/en-us/library/system.io.file.writeallbytes(v = vs .1px).aspx),并且OP似乎根本没有实际使用FileHelper,也许正因为如此,并且正在调用'File.WriteAllBytes(path,array);'来代替。 – stuartd

+0

Ups,没有注意到。那么如果他使用filehelper,它应该工作tbh。 – pajamac

+0

所以现在它打印文本,但只在一行。但我希望它成为新行中的每个句子 – dotDREAMS

相关问题