I want to create a txt file based on certain inputs.
To create a text file you can use the
StreamWriter Class (System.IO)[
^]
if statements looking at the specific cases and if true: Output a specific paragraph or sentence into the txt file.
if(SpecificCase)
{
MyStreamWriter.WriteLine("A specific paragraph or sentence.");
}
How can I build it so when some cases dont exist it doesn't just leave a lot of white space.
Edit: White lines are caused by calling "WriteLine" and not adding to the new line. So, I advise you to just not call "WriteLine" or "Write" if it's empty and you don't want just an empty space.
Here's an example by using a switch statement:
using(StreamWriter sw = new StreamWriter("myTextFile.txt"))
{
string LineToWrite = String.Empty;
switch(SpecificCase)
{
case "Case1":
LineToWrite = "A specific message";
break;
case "Case2":
LineToWrite = "A different Message";
break;
}
sw.WriteLine(LineToWrite);
}
Edit: If you use "case default" you can basically insert any code you'd add to the "else" after a couple of "if" switches. It's the case it will always do if no other match has been found if I'm not mistaken. More information can be found at
http://www.dotnetperls.com/switch[
^]
While reading or writing using the stream writer be sure to use a "using statement" to make sure your StreamWriter disposes properly.
Best Regards,
- Eddie