locked
C# Add String to String[] And save to File RRS feed

  • Question

  • Hello i want save Output value to file. I've got:

    int Count = ListBox.Items.Count;
    string GameName;
    string[] Lines;
    int LineCount = 0;
    string[] Messages;
    string Output;
    string str;
    if (Count >= 1)
    {
        for(int i = Count; i >= 1 ;)
        {
            LineCount = 0;
            GameName = ListBox.GetItemText(ListBox.Items[i - 1]);
            Lines = File.ReadAllLines(path + @"\Games\" + GameName + ".txt");
            foreach(string line in Lines)
            {
                 LineCount += 1;
            }
            i--;
            str = (GameName + " [" + LineCount + "]");
            MessageBox.Show(str);
        }
        Output = Messages.ToString();
        File.WriteAllLines(path + @"\Games\" + "Output.txt", new[] {Output});
    
    }

    How can i Add str to Messages and than set Output same as Messages, save Output to file. I was trying something with List<string> and Messages.Add(str) But it didnt work.

    https://imgur.com/a/3ZyWeil

    Thank you.




    Thursday, November 1, 2018 12:57 PM

Answers

  • You are not instantiating the Messages field as it is pointing to null  you are getting Null Reference Exception. So change the following line :

    List<string> Messages = null;


    to:

    List<string> Messages = new List<string>();

    and then you can write it to file like :

    File.WriteAllLines(path + @"\Games\" + "Output.txt", Messages.ToArray());

    Hope it helps!


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites



    Thursday, November 1, 2018 1:19 PM

All replies

  • You are not instantiating the Messages field as it is pointing to null  you are getting Null Reference Exception. So change the following line :

    List<string> Messages = null;


    to:

    List<string> Messages = new List<string>();

    and then you can write it to file like :

    File.WriteAllLines(path + @"\Games\" + "Output.txt", Messages.ToArray());

    Hope it helps!


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites



    Thursday, November 1, 2018 1:19 PM
  • Hi,

    you need to create a new instance of List. 

    List<string> list = new List<string>();

    After that you can call Add.

    Greetings, Chris

    Thursday, November 1, 2018 1:23 PM
  • Thank you, that was part of that what i needed!
    Thursday, November 1, 2018 1:31 PM
  • Thanks, it works :)
    Thursday, November 1, 2018 1:33 PM