margarit
asked on
Write string to text file C#
Hello,
I have a long text string and I need to save it to text file.
I have "Save" button and string. How to do it? How to open a file a nd write to it? I never worked with files.
THANKS
I have a long text string and I need to save it to text file.
I have "Save" button and string. How to do it? How to open a file a nd write to it? I never worked with files.
THANKS
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://www.devhood.com/Tutorials/tutorial_details.aspx?tutorial_id=400
Modify this script so that when your user clicks save, the WriteToFile Function is called. aRandomVariableContainingM oreText (Below) can be replaced by the contents of wherever your users are typing like SW.WriteLine(Text1.text) or similar
StreamWriter SW;
SW=File.CreateText("c:\\MyTextFile.txt");
SW.WriteLine("This is your first line of text");
SW.WriteLine(aRandomVariableContainingMoreText);
SW.Close();
PS, a good source for working with text files in C# is:
http://www.csharphelp.com/archives/archive24.html
I used it when i learned and I would recommend it to anyone :)
Ben
http://www.csharphelp.com/archives/archive24.html
I used it when i learned and I would recommend it to anyone :)
Ben
ASKER
Hello,
Thanks for all the fast answers!!!
If my string contains "\n" - new line. Would it go to a new line in a text file. If not how to do it?
THANKS
Thanks for all the fast answers!!!
If my string contains "\n" - new line. Would it go to a new line in a text file. If not how to do it?
THANKS
You need to split the string into an array of strings.
string testString = "This\nis\njust\na\ntest\nof\nmultiple\nlines.";
string[] lines = testString.Split(new char[] { '\n' });
File.WriteAllLines(@"C:\Temp\testFile.txt", lines);
i don't think so..
if you want only add something to file then:
and..
WOW how many fast answers :)
if you want only add something to file then:
and..
WOW how many fast answers :)
StreamWriter sw = new StreamWriter(@"C:\test.txt", true)
sw.Write(yourString);
sw.WriteLine(yourString2);
ASKER
Hello!
THANKS AGAIN!!!
I use the following method to write to file:
StreamWriter sw = new StreamWriter(@"C:\test2.tx t", true);
The problem that if the file test2 already exists it does not rewrite it. It append my text to already existed text in the file.
How can I delete the text in file? Or re-create the file?
THANKS
THANKS AGAIN!!!
I use the following method to write to file:
StreamWriter sw = new StreamWriter(@"C:\test2.tx
The problem that if the file test2 already exists it does not rewrite it. It append my text to already existed text in the file.
How can I delete the text in file? Or re-create the file?
THANKS
try
StreamWriter sw = new StreamWriter(@"C:\test2.tx
t", false);
StreamWriter sw = new StreamWriter(@"C:\test2.tx
t", false);
You need to use FileStream class for that
FileStream fs = new FileStream(@"C:\test2.txt" , FileMode.CreateNew, FileAccess.Write, FileShare.Read);
StreamWriter sw = new StreamWriter(fs);
This will recreate the file.
FileStream fs = new FileStream(@"C:\test2.txt"
StreamWriter sw = new StreamWriter(fs);
This will recreate the file.
Oops!
Just a small typo. It should be FileMode.Create instead of FileMode.CreateNew.
Just a small typo. It should be FileMode.Create instead of FileMode.CreateNew.
FileStream fs = new FileStream(@"C:\test2.txt", FileMode.Create, FileAccess.Write, FileShare.Read);
StreamWriter sw = new StreamWriter(fs);
ASKER
Hello!
THANKS AGAIN!!!
I tried StreamWriter sw = new StreamWriter(@"C:\test2.tx
t", false);
and it works fine
THANKS A LOT!!!!
THANKS AGAIN!!!
I tried StreamWriter sw = new StreamWriter(@"C:\test2.tx
t", false);
and it works fine
THANKS A LOT!!!!
but this one create new or/and recreate file (checked)
StreamWriter sw = new StreamWriter(@"C:\test2.tx t", false);
but i think CuteBug found beter solution:
and its creating new file and rewriting also
StreamWriter sw = new StreamWriter(@"C:\test2.tx
but i think CuteBug found beter solution:
and its creating new file and rewriting also
File.WriteAllText(@"c:\s.txt", yourString);
ASKER
THANKS A LOT!!! YOU HELPED ME A LOT
@margarit
my pleasure! :)
my pleasure! :)