2017-09-01 47 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

相關問題