I want to create .data file in C# in the following format: -
#Section Name
data
data
data
#Section Name
data
data
data
.
.
.
Can anyone help me out in this regard.
I want to create .data file in C# in the following format: -
#Section Name
data
data
data
#Section Name
data
data
data
.
.
.
Can anyone help me out in this regard.
You might be trying to open the file more then once inside your code, that would lead to such exception.
Another approach is using a StringBuilder
instead of using StreamWriter
to write the content, and then just use System.IO.File.WriteAllText
to write the content of the StringBuilder
to a file.
Using a StringBuilder
will probably have higher memory use but lower IO use then using a StreamWriter
. for more details, read this.
Here is how I would do it using a StringBuilder
:
string FilePath = "c:/temp.data"; // this should hold the full path of the file
StringBuilder sb = new StringBuilder();
// Of course, you should use your one code to create the data,
// this example is purely based on your format description.
sb.AppendLine("#Section Name").AppendLine();
sb.AppendLine("data");
sb.AppendLine("data");
sb.AppendLine("data").AppendLine();
File.WriteAllText(FilePath, sb.ToString());