[Solved] Use String.Format with local variables C# [closed]


Well, first, this is not the way to get the path to the desktop directory. Instead, just do this:

Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)

Unlike your solution, this will work with folder redirection, remote profiles, as well as with future (and past!) Windows versions. Not to mention that Environment.Username isn’t necessarily the correct folder name anyway 🙂

Second,

var subdir = "SomeDir";
File.WriteAllLines(string.Format(@"C:\SomeParent\{0}\MyFile.txt", subdir), lines);

You need to actually call the string.Format method. If you just add another argument to File.WriteAllLines itself… well, how would that work? Are you also writing all your methods to accept a formattable string with a variable amount of arguments?

The somewhat cryptic error message is just trying to tell you that there isn’t a method overload that fits your arguments – the closest it can get results in the error shown.

solved Use String.Format with local variables C# [closed]