簡體   English   中英

寫入文本文件的問題

[英]Issue with writing to a text file

我正在編寫一個想要添加Surfer (該類需要四個字符串, FirstNameLastNameDobCountry )。 (我不使用DateTime因為雖然使用它可能是更好的做法,但對於該項目不是必需的。)然后,我想將剛創建的類寫入文本文件,並且為此使用了格式化的字符串。 我有兩個問題。

  1. 它沒有將DobCountry寫入文本文件。

  2. 我不確定如何將每個條目放在新行中。 (我曾經嘗試\\ n我的FileFormat字符串的結尾和seperately在我的字符串的結尾toFile

我嘗試執行此操作的代碼是

public class Surfer
{
    protected string SurferFirstName { get; set; }
    protected string SurferLastName { get; set; }
    protected string SurferDob { get; set; }
    protected string SurferCountry { get; set; }
    public Surfer() { }

    public Surfer(string surferFirstName, string surferLastName, string surferDob, string surferCountry)
    {
        SurferFirstName = surferFirstName;
        SurferLastName = surferLastName;
        surferDob = SurferDob;
        surferCountry = SurferCountry;
    }
    public override string ToString()
    {
        return string.Format("Surfer name:  {0} {1}   Surferdob:  {2} \n Country:  {3}", SurferFirstName, SurferLastName, SurferDob, SurferCountry);
    }
    //for display in the textblock, virtual for inheritance
   /* public virtual string vehicleDetails()
    {
        return string.Format("Make:{0,40} \nModel:{1,40} \nPrice:{2,40} \nYear: {3,40} \nColour: {4,40}, \nMileage: {5,40} \nDescription: {6,40} \nEngine: {7,40}", Make, Model, Price, Year, Colour, Mileage, Description, Engine);
    }*/
    //for formatting to a text file
    public string FileFormat()
    {
        return string.Format("{0},{1},{2},{3} \n", SurferFirstName, SurferLastName, SurferDob, SurferCountry);

    }
}

我的代碼是WindowAdd.cs

private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
    //reading in from text boxes
    string fName = txtbxFirstName.Text;
    string lName = txtbxLastName.Text;
    string dob = txtbxDob.Text;
    string country = txtbxCountry.Text;
    //basic validation
    if (fName.Length <2 || lName.Length <2 )
    { 
        MessageBox.Show("first and last name must be entered");
    }
    else if(dob.Length <10 || dob.Length>10)
        MessageBox.Show("Enter dob in the correct format (dd/mm/yyyy)");
    else if(country.Length <3)
        MessageBox.Show("Enter a valid country");
    else
    {
        try
        {
            //sets mainWindow
            MainWindow mainWindow = new MainWindow();
            //creates new surfer class
            Surfer newSurfer = new Surfer(fName, lName, dob, country);
            //adds to observable collection
            mainWindow.surfers.Add(newSurfer);
            //uses FileFormat from the Surfer Class to format in order to add to text file
            string toFile = newSurfer.FileFormat();
            //append to Surfers.Text
            File.AppendAllText("Surfers.Text", toFile);

            //closes window
            this.Close();
        }

我在文本文件中得到的結果是這樣的

約翰·巴里(John,Barry),快樂吉爾莫(Gilmore),

這是兩個嘗試的條目

基本驗證正在運行,因此可以從xaml中讀取數據。 我知道我可以使用File.WriteAllLines並編寫代碼以通過保存按鈕將可觀察到的集合中的每個元素發送到文本文件,但是如果可能的話,我寧願以與嘗試的方法類似的方式進行操作,在此保存一次只輸入一個文本文件,而不會刪除現有元素。

您的構造函數將變量分配給Dob和Country的方向錯誤。 您將屬性分配給參數,而不是參數分配給屬性。

public Surfer(string surferFirstName, string surferLastName, string surferDob, string surferCountry)
        {
            SurferFirstName = surferFirstName;
            SurferLastName = surferLastName;
            surferDob = SurferDob;
            surferCountry = SurferCountry;
        }

對於新行,請使用Quantic上面建議的Environment.NewLine

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM