I'm trying to write binary user data into a .data file and for bw.Write() the substitutes {0},{1},{2},{3} cannot be used because of it being mistakened to be a parameter to the method.
Is there a work around to write this data to the file but still substituting variables like in Console.Write("Hello {0}", name)
When I hover over the error it says
No overload for method 'Write' takes 5 arguments
public static void WriteUser(String path, String user, String auth, String pass, DateTime date)
{
try
{
BinaryWriter bw = new BinaryWriter(new FileStream("mydata", FileMode.Open));
bw.Write("{0}{\nAuth:{1}\nPass:{2}\nDateCreated:{3}", user, auth, pass, date);
}
catch (IOException e)
{
Console.WriteLine(e.Message + "\n Cannot write to file.");
return;
}
bw.Close();
}
Just call string.Format
yourself:
bw.Write(string.Format("{0}{\nAuth:{1}\nPass:{2}\nDateCreated:{3}", user, auth, pass, date));
Although if you're using C# 6, you can make this cleaner using interpolated strings:
bw.Write($"{user}{{\nAuth:{auth}\nPass:{pass}\nDateCreated:{date}");
Note that if you're trying to write text, I'd just use StreamWriter
- or better yet, just call File.WriteAllText
:
public static void WriteUser(String path, String user, String auth, String pass, DateTime date)
{
try
{
File.WriteAllText(
"mydata",
$"{user}{{\nAuth:{auth}\nPass:{pass}\nDateCreated:{date}");
}
catch (IOException e)
{
Console.WriteLine(e.Message + "\n Cannot write to file.");
return;
}
}
See more on this question at Stackoverflow