-1

I am in the process of learning C# and hit a wall that shows I'm obviously missing something important. The line:

var objWriter = new System.IO.StreamWriter(fileName, False);

In the code below causes an error - the string variable fileName can't be converted to System.IO.Stream and False doesn't exist in the current context. Why?

string message = "Hi There!";
string myDocs = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string fileName = myDocs + "'\'Test.txt";

if (!System.IO.File.Exists(fileName))
{
    System.IO.File.Create(fileName).Dispose();
}
var objWriter = new System.IO.StreamWriter(fileName, False);
objWriter.Write(message);
Console.WriteLine("Message Saved");
objWriter.Close();
martijnn2008
  • 3,552
  • 5
  • 30
  • 40
Beginner
  • 129
  • 10
  • 7
    c# is case sensitive, so it's `false`, not `False` – Kolichikov Sep 12 '17 at 19:04
  • 1
    Arghhhhhhh. Its false and not False – Beginner Sep 12 '17 at 19:07
  • 4
    I advise to use the `using` pattern, so you can't forget to `.Close()` your `StreamWriter` https://stackoverflow.com/questions/513672/disposable-using-pattern – martijnn2008 Sep 12 '17 at 19:08
  • @Kolichikov Well, there's a painful and very time consuming lesson learnt. Many thanks – Beginner Sep 12 '17 at 19:08
  • Since I wasted one of my few precious questions on this, could anyone suggest a way of making the code more concise. Perhaps a really useful blog or article?? – Beginner Sep 12 '17 at 19:13
  • @martijnn2008 Thank you. Good link – Beginner Sep 12 '17 at 19:14
  • Heres a link to the guide for writing to files in .net from MSDN. You only need the first example: https://learn.microsoft.com/en-us/dotnet/standard/io/how-to-write-text-to-a-file (and make sure to ignore the VB one) – Kolichikov Sep 12 '17 at 19:15
  • Maybe this would have been a better link in my comment: https://stackoverflow.com/questions/518352/does-dispose-still-get-called-when-exception-is-thrown-inside-of-a-using-stateme?noredirect=1&lq=1 But I am glad you appreciated it ;) – martijnn2008 Sep 12 '17 at 19:15

3 Answers3

3

Fix: Replace False with false.

Explanation: Here is a list of constructors that StreamWriter has. Notice that it either takes a Stream and an Encoding, or a String and Boolean.

Since C# is case sensitive, it tries to look for an object called False somewhere in your code, which explains your first issue (False doesn't exist in the current context). But False is an object, not a boolean, so the compiler assumes that fileName is of type Stream (to fit with the signature), but alas, it doesn't know how to convert your string to a Stream, hence the second error.

Kolichikov
  • 2,944
  • 31
  • 46
1

Youre \ isn´t working in your given code and replace False with false

Replace this

    string fileName = myDocs + "'\'Test.txt";

with this

    string fileName = myDocs + @"\Test.txt";
Florian3007
  • 87
  • 14
0

The code I would suggest

var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Test.txt");

using (var fs = new StreamWriter(path))
{
    fs.Write("Hi there!");
}

Console.WriteLine("Message Saved");

First thing, please use Path.Combine static method to create full path. Dealing with \ or \\ will also work, but is is much prettier to use already created method.

Second thing is that it is always good to create IDisposable object within using block. In your case using will close your stream at the end of using block scope.

Another remark is that, it is always better to declare variable in a place where you want to use it. In that case "Hi there!" string is created directly in a place of usage. That is one of the good practices we should obey. Please look at that link.

rraszewski
  • 1,135
  • 7
  • 21
  • I think you should explain why you suggest the code snippet you suggested to get your answer up voted. – martijnn2008 Sep 12 '17 at 19:18
  • @martijnn2008 Sorry, not sure what you mean. I up-voted the Floarian3007 as he wisely spotted that I made a mess of my concatenation. I really like rraszewski suggestion too as it has opened up some interesting research for me – Beginner Sep 12 '17 at 19:32
  • I guess you would need using.System.IO; to make this solution work – Beginner Sep 12 '17 at 19:34
  • @Rich First of all this commented is not written to you. Second, I am missing the "why" in his answer. Of course now you can Google the "why". I also don't like the long line of code, especially while the question uses these nice intermediate variables. Additionally, the `true` parameter for the append-mode is missing. – martijnn2008 Sep 12 '17 at 19:43