0
var output = query.Select(x => x.Plant + "\t" + x.Animal + "\t" + blah blah blah);
string FilePath = @"C:\output.txt";
File.WriteAllLines(FilePath, output);

The error I get when switching the target framework from 4.0 to 3.5 is:

Error 14 The best overloaded method match for 'System.IO.File.WriteAllLines(string, string[])' has some invalid arguments

What is the easiest way to make the switch to the 3.5 Framework without having to change too much code and still keep its functionality?

nawfal
  • 70,104
  • 56
  • 326
  • 368
MrPatterns
  • 4,184
  • 27
  • 65
  • 85
  • 4
    check msdn http://msdn.microsoft.com/en-us/library/system.io.file.writealllines(v=vs.90).aspx – Ric Nov 01 '13 at 14:53
  • 1
    You really couldn't leverage the answers to the question you asked yesterday to do this? http://stackoverflow.com/questions/19715640/is-there-a-net-3-5-equivalent-of-4-0s-appendalllines/19715927#19715927 – Servy Nov 01 '13 at 15:03
  • @Servy I'm confused because I thought Append means that nothing is deleted. The text is merely added. However, here, the way I used WriteAllLines in my old code was to overwrite what was there before. In my mind, it was a different question because there's a distinction between appending and overwriting. – MrPatterns Nov 01 '13 at 15:14
  • @phan Yes, it's not technically an exact duplicate, which is why I didn't vote to close as a duplicate, but it's almost exactly the same, and changing either of the working answers to your previous question to truncate instead of appending is a trivial task. There was no need to ask a new question to get that behavior. Did you even try to get modify one of those answers to overwrite instead? – Servy Nov 01 '13 at 15:16
  • @Servy For you it was trivial. For me it was something I could not think up easily. It's like I'm a 1 year old with limited vocabulary. I know what I want and what I mean, but I don't know how to say it. I could not express (in code) what, for you, comes pretty naturally. Btw, I don't code for a living and it's nowhere near required for my work, I just teach myself just enough to make some parts of my life a little easier. – MrPatterns Nov 01 '13 at 15:31
  • @phan Did you try? Did you look at the methods being used for alternatives that would overwrite instead of appending? Yes, it might take you a few minutes of your time instead of a few seconds, but it's still something that's entirely doable for you. It sounds like you gave up before you even started. – Servy Nov 01 '13 at 15:34
  • 1
    @Servy How do you know what's entirely doable for me? Look, I appreciate what you're trying to do. I think you have good intentions and are trying to get me to think harder/longer and figure things out for myself more. If that's the case, the best way to achieve that is to stop answering my questions which forces me to do it on my own. I won't take offense when you ignore my questions. In the meantime, I will continue to learn this way by asking simple questions that a lot of other people here can help me with. Believe it or not, this process of asking/answering works for me. – MrPatterns Nov 01 '13 at 15:44
  • @phan Yet such behavior is not considered acceptable according to the site's standards. They require that users spend time attempting to solve their own problems, demonstrate research effort, and their own attempted solutions, etc. when posting questions. You may want to just ask other people to do all of your work for you, but that is not what this site is here for. How do you know that this *isn't* doable for you? You didn't even try. Given that you didn't try, you don't know that you can't do it. If you had tried, and been unable to solve it, *then* you would have known. – Servy Nov 01 '13 at 15:48
  • @Servy LOL, you make a lot of assumptions. I did try. I think the real question you want answered is, Did I try "hard enough" to meet your standards? Did I spend "enough time" according to you attempting to solve my own problem? Servy, I appreciate where you're coming from but just ignore my questions from now on. I can avoid your preachiness and you can avoid "doing all my work". Take a look at my history, you will see I ask questions in spurts, as needed. I never was, and never will be, on the path to becoming a programmer. That just isn't my goal. – MrPatterns Nov 01 '13 at 16:00

2 Answers2

6

The .NET 3.5 overload of File.WriteAllLines accepts a string[]. In .NET 4.0 there's an overload for IEnumerable<string>, which is why your original code works. Use an array instead and you should be fine:

File.WriteAllLines(FilePath, output.ToArray());
Ahmad Mageed
  • 94,561
  • 19
  • 163
  • 174
  • However this forced him to bring the entire file's contents into memory, which can be a problem. What if he doesn't even have enough memory to do that? And even if he does, performance can suffer as a result. – Servy Nov 01 '13 at 15:03
  • @Servy that's a valid point but on the surface this is the closest to an equivalent to get the method working using what .NET 3.5 gives you. Sure, rewriting to take that into account is another option, but unless memory is a concern it's not necessary. Not sure if you downvoted as a result, but the answer isn't wrong. – Ahmad Mageed Nov 01 '13 at 15:24
  • The question specifically stated, "and still keep its functionality". This does not do that, which does in fact make it wrong. It's actually quite common when dealing with files for there to be large amounts of data such that bringing it all into memory needlessly can very frequently be a problem, this is not something that can be easily ignored. Given that a proper solution takes very, very little code, in order to provide correct semantics, there is no reason to be doing something like this. – Servy Nov 01 '13 at 15:27
  • @Servy I'm not debating how to handle large amounts of data. "Still keep its functionality" to the OP probably means "make this method work" - not the memory management and lazy loading aspects of the problem. Your view is pedantic, which is somewhat applaudable, but let's not lose sight of helping the OP. I find your concern valid, and [Matthew Watson's answer](http://stackoverflow.com/a/19729400/59111) addresses it, but calling it "wrong" is narrow sighted. Unless you claim to know what the OP really meant, clearly there's a grey area here and either answer "works." – Ahmad Mageed Nov 01 '13 at 15:53
  • Your answer might, possibly, maybe work, with *just* poorer performance, in the event that the OP wasn't actually leveraging the functionality of the method he is asking to replicate. In such a situation your answer may be an acceptable alternative despite the fact that it is not correct. That or you could take the extra 30 seconds to write a method that actually works without needing to make any assumptions about situations not provided, and that does exactly what the stated method does. – Servy Nov 01 '13 at 15:57
  • Guys guys, simmer down. Servy is a really smart dude with stringent standards, and he's maybe a little prickly. Let's just move on. Servy I won't be offended if you refuse to answer any more of my questions and "do all of my work". I understand now that my questions and "all of my work" may be burdensome for you. – MrPatterns Nov 01 '13 at 16:17
1

If you are writing a huge number of lines and you therefore don't want to use .ToArray(), just use this method:

public static void MyWriteAllLines(string filename, IEnumerable<string> lines)
{
    using (var writer = new StreamWriter(filename))
    {
        foreach (var line in lines)
            writer.WriteLine(line);
    }
}
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276