Click here to Skip to main content
15,890,982 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello All,

I have come across this problem.

I have to write a huge string into a file. For this I use the string builder object(using stringbuilder.append method) and assign the huge string to it and then, using stream writer, I write it onto the file.

It works fine but mainting the code is not easy and the code looks dirty. Can any one tell me if this can be done in a simple way?
Please let me know if you need any other inputs?

snippet
C#
StringBuilder UptoMain = new StringBuilder();
StreamWriter file = new StreamWriter(@"c:\temp.txt", false);
UptoMain.AppendLine(@"Here there is lots and lots of text which makes 
the code look very ugly and and messy to hande");
file.WriteLine(UptoMain);
Posted
Updated 13-Sep-10 21:15pm
v3
Comments
Per Söderlund 14-Sep-10 1:12am    
Could you show us your code?
Maybe we can help you with it, because i don't fully understand what your problem is.
asjadazeez 14-Sep-10 1:31am    
I have added the snippet to the question
Dalek Dave 14-Sep-10 3:16am    
Edited for Grammar and Readability.
Abhishek Sur 14-Sep-10 4:33am    
From Mohangbits :
your code looks superb :P

ultimately what matters is the functionality. Code is working or not

You could start by using the text file you create as the output to be the input as well. Of course this would mean that for the first time, your input is the text file and not a "hard coded" string builder.
 
Share this answer
 
Comments
asjadazeez 14-Sep-10 0:57am    
Thanks for your reply .What i understand from you solution is that you want me to read the string from a text file and use then write it again into the target file. but this does not serve the purpose what i am looking for because i dont want to have another file.
Why do you need to hard-wire the strings in you program? Cannot you read them from a 'configuration' file?
:)
 
Share this answer
 
Comments
asjadazeez 14-Sep-10 3:35am    
Objective is to create some file with a fixed data. Now if that data is itself to be copied from another file well i dont want it because there is a necessity that i do some sort of modification and more over i dont want the user to see this text and keeping a config file is not convinient.
CPallini 14-Sep-10 4:09am    
If you need some sort of modification, then a XML file might be a good solution. If you really need to hide the text then embedding it in the executable is not enough.
I think you don't need that StringBuilder at all!

The following code:

C#
StringBuilder UptoMain = new StringBuilder();
StreamWriter file = new StreamWriter(@"c:\temp.txt", false);
UptoMain.AppendLine(@"Here there is lots and lots of text which makes the code look very ugly and and messy to hande");
file.WriteLine(UptoMain);


can be reduced to:

C#
StreamWriter file = new StreamWriter(@"c:\temp.txt", false);
string s = @"Here there is lots and lots of text which makes the code look very ugly and and messy to hande";
file.WriteLine(s);


This does not sort your problem with the long string though... If you really need to keep the string in code, all you can do is to move the string constant somewhere else. You can write something like:

C#
private const string MyString = @"Here there is lots and lots of text which makes the code look very ugly and and messy to hande";


...and then use MyString in the code later. This will make your code more readable, since you can move the string constant declaration anywhere you like, even to a separate file.
 
Share this answer
 
Comments
asjadazeez 14-Sep-10 4:05am    
String builder is used because other operations and methods of string builder are used(example appending other strings) . so the code solution provided by you is not applicable in my case. so that is not the solution i am looking for.
You might make it a bit moduler.

public void WriteFile(string filename, string text)
{
    using(StreamWriter file = new StreamWriter(filename, false))
    {
         file.WriteLine(text);
    }

}


From your application you call this method using :

StringBuilder UptoMain = new StringBuilder();
UptoMain.AppendLine(....);
this.WriteFile(@"c:\temp.txt", UptoMain.ToString());


Using is essential as it disposes the object after you are done writing.
:thumbsup:
 
Share this answer
 
Comments
asjadazeez 14-Sep-10 4:57am    
Seems like my question is not clearly understood.Modular programing is done. But what is shown is just a snippet example.!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900