-1

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,149
  • 12
  • 28
  • 32
Harsh
  • 173
  • 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)_ –  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

2 Answers2

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
  • 79,642
  • 10
  • 69
  • 121
0

You need to open a stream with FileShare.ReadWrite options.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
cucu
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 17 '22 at 17:36