0

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.

dario
  • 5,021
  • 12
  • 27
  • 32
Harsh
  • 155
  • 1
  • 3
  • 11
  • 1
    Welcome to SO. Your question sort of reads as a set of requirements. Any code to show? What research have you done? These things will help us help you. Good luck! _[How do I ask a good question?](http://stackoverflow.com/help/how-to-ask)_ – MickyD Jun 03 '15 at 05:31
  • 1
    What have you done so far? – ckruczek Jun 03 '15 at 05:31
  • I tried to create this file by using StreamWriter class, but while using this class, it gives me "The file is being used by another process" error. I am more concerned about the formatting of the file. – Harsh Jun 03 '15 at 05:33
  • 1
    Show us what you have done so far. Formatting the file should be pretty easy. StreamWriter is a good approach, you just need to format the string you write into the file with `String.Format` – ckruczek Jun 03 '15 at 05:34
  • show us the code you are doing so that we can help. – jomsk1e Jun 03 '15 at 06:11

1 Answers1

0

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());
Community
  • 1
  • 1
Zohar Peled
  • 76,346
  • 9
  • 62
  • 111