There are a lot of different ways to read and write files (text files, not binary) in C#.
I just need something that is easy and uses the least amount of code, because I am going to be working with files a lot in my project. I only need something for string since all I need is to read and write strings.
Use File.ReadAllText and File.WriteAllText.
MSDN example excerpt:
// Create a file to write to.
string createText = "Hello and Welcome" + Environment.NewLine;
File.WriteAllText(path, createText);
...
// Open the file to read from.
string readText = File.ReadAllText(path);
In addition to File.ReadAllText, File.ReadAllLines, and File.WriteAllText (and similar helpers from File class) shown in another answer you can use StreamWriter/StreamReader classes.
Writing a text file:
using(StreamWriter writetext = new StreamWriter("write.txt"))
{
writetext.WriteLine("writing in text file");
}
Reading a text file:
using(StreamReader readtext = new StreamReader("readme.txt"))
{
string readText = readtext.ReadLine();
}
Notes:
You can use readtext.Dispose() instead of using, but it will not close file/reader/writer in case of exceptions
Be aware that relative path is relative to current working directory. You may want to use/construct absolute path.
Missing using/Close is very common reason of "why data is not written to file".
FileStream fs = new FileStream(txtSourcePath.Text,FileMode.Open, FileAccess.Read);
using(StreamReader sr = new StreamReader(fs))
{
using (StreamWriter sw = new StreamWriter(Destination))
{
sw.Writeline("Your text");
}
}
The easiest way to read from a file and write to a file:
//Read from a file
string something = File.ReadAllText("C:\\Rfile.txt");
//Write to a file
using (StreamWriter writer = new StreamWriter("Wfile.txt"))
{
writer.WriteLine(something);
}
using (var file = File.Create("pricequote.txt"))
{
...........
}
using (var file = File.OpenRead("pricequote.txt"))
{
..........
}
Simple, easy and also disposes/cleans up the object once you are done with it.
#AlexeiLevenkov pointed me at another "easiest way" namely the extension method. It takes just a little coding, then provides the absolute easiest way to read/write, plus it offers the flexibility to create variations according to your personal needs. Here is a complete example:
This defines the extension method on the string type. Note that the only thing that really matters is the function argument with extra keyword this, that makes it refer to the object that the method is attached to. The class name does not matter; the class and method must be declared static.
using System.IO;//File, Directory, Path
namespace Lib
{
/// <summary>
/// Handy string methods
/// </summary>
public static class Strings
{
/// <summary>
/// Extension method to write the string Str to a file
/// </summary>
/// <param name="Str"></param>
/// <param name="Filename"></param>
public static void WriteToFile(this string Str, string Filename)
{
File.WriteAllText(Filename, Str);
return;
}
// of course you could add other useful string methods...
}//end class
}//end ns
This is how to use the string extension method, note that it refers automagically to the class Strings:
using Lib;//(extension) method(s) for string
namespace ConsoleApp_Sandbox
{
class Program
{
static void Main(string[] args)
{
"Hello World!".WriteToFile(#"c:\temp\helloworld.txt");
return;
}
}//end class
}//end ns
I would never have found this myself, but it works great, so I wanted to share this. Have fun!
These are the best and most commonly used methods for writing to and reading from files:
using System.IO;
File.AppendAllText(sFilePathAndName, sTextToWrite);//add text to existing file
File.WriteAllText(sFilePathAndName, sTextToWrite);//will overwrite the text in the existing file. If the file doesn't exist, it will create it.
File.ReadAllText(sFilePathAndName);
The old way, which I was taught in college was to use stream reader/stream writer, but the File I/O methods are less clunky and require fewer lines of code. You can type in "File." in your IDE (make sure you include the System.IO import statement) and see all the methods available. Below are example methods for reading/writing strings to/from text files (.txt.) using a Windows Forms App.
Append text to an existing file:
private void AppendTextToExistingFile_Click(object sender, EventArgs e)
{
string sTextToAppend = txtMainUserInput.Text;
//first, check to make sure that the user entered something in the text box.
if (sTextToAppend == "" || sTextToAppend == null)
{MessageBox.Show("You did not enter any text. Please try again");}
else
{
string sFilePathAndName = getFileNameFromUser();// opens the file dailog; user selects a file (.txt filter) and the method returns a path\filename.txt as string.
if (sFilePathAndName == "" || sFilePathAndName == null)
{
//MessageBox.Show("You cancalled"); //DO NOTHING
}
else
{
sTextToAppend = ("\r\n" + sTextToAppend);//create a new line for the new text
File.AppendAllText(sFilePathAndName, sTextToAppend);
string sFileNameOnly = sFilePathAndName.Substring(sFilePathAndName.LastIndexOf('\\') + 1);
MessageBox.Show("Your new text has been appended to " + sFileNameOnly);
}//end nested if/else
}//end if/else
}//end method AppendTextToExistingFile_Click
Get file name from the user via file explorer/open file dialog (you will need this to select existing files).
private string getFileNameFromUser()//returns file path\name
{
string sFileNameAndPath = "";
OpenFileDialog fd = new OpenFileDialog();
fd.Title = "Select file";
fd.Filter = "TXT files|*.txt";
fd.InitialDirectory = Environment.CurrentDirectory;
if (fd.ShowDialog() == DialogResult.OK)
{
sFileNameAndPath = (fd.FileName.ToString());
}
return sFileNameAndPath;
}//end method getFileNameFromUser
Get text from an existing file:
private void btnGetTextFromExistingFile_Click(object sender, EventArgs e)
{
string sFileNameAndPath = getFileNameFromUser();
txtMainUserInput.Text = File.ReadAllText(sFileNameAndPath); //display the text
}
Or, if you are really about lines:
System.IO.File also contains a static method WriteAllLines, so you could do:
IList<string> myLines = new List<string>()
{
"line1",
"line2",
"line3",
};
File.WriteAllLines("./foo", myLines);
It's good when reading to use the OpenFileDialog control to browse to any file you want to read. Find the code below:
Don't forget to add the following using statement to read files: using System.IO;
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Text = File.ReadAllText(openFileDialog1.FileName);
}
}
To write files you can use the method File.WriteAllText.
class Program
{
public static void Main()
{
//To write in a txt file
File.WriteAllText("C:\\Users\\HP\\Desktop\\c#file.txt", "Hello and Welcome");
//To Read from a txt file & print on console
string copyTxt = File.ReadAllText("C:\\Users\\HP\\Desktop\\c#file.txt");
Console.Out.WriteLine("{0}",copyTxt);
}
}
private void Form1_Load(object sender, EventArgs e)
{
//Write a file
string text = "The text inside the file.";
System.IO.File.WriteAllText("file_name.txt", text);
//Read a file
string read = System.IO.File.ReadAllText("file_name.txt");
MessageBox.Show(read); //Display text in the file
}
Reading from file
string filePath = #"YOUR PATH";
List<string> lines = File.ReadAllLines(filePath).ToList();
Writing to file
List<string> lines = new List<string>();
string a = "Something to be written"
lines.Add(a);
File.WriteAllLines(filePath, lines);
Simply:
String inputText = "Hello World!";
File.WriteAllText("yourfile.ext",inputText); //writing
var outputText = File.ReadAllText("yourfile.ext"); //reading
You're looking for the File, StreamWriter, and StreamReader classes.
I know this is a bit of a "Day one" question, but I'm still having trouble understanding why my Stream writer is writing empty lines after each time it writes
namespace PostFinder
{
class HistorySaver
{
public static void Save(string item, string path)
{
StreamReader sre = new StreamReader(path);
string historyList = sre.ReadToEnd();
sre.Dispose();
StreamWriter sr = new StreamWriter(path);
sr.WriteLine(historyList+sr.NewLine+item);
sr.Dispose();
}
}
}
sr.WriteLine(historyList+sr.NewLine+item);
The .WriteLine() method puts an end-of-line character after the contents you pass to it. If you don't want that character, use .Write().
It looks like all you are wanting to do is append text to a file, so there is really no need to open a streamreader to read in the existing contents and then write them back out with your new content.
You can use the below to do all that you want in one step. If the input path file doesn't exist it will create a new one, and if it already exists it will just append your new item.
namespace PostFinder
{
class HistorySaver
{
public static void Save(string item, string path)
{
File.AppendAllText(path, item + Environment.NewLine);
}
}
}
I have trying to mirror the output screen to .txt file.By my below code i can able to mirror the output screen to text file. When executing the obj.OutputFile("First text"); there is no problem But some times i need print like obj.OutputFile("Second text {0}",text);
I got the exception during the second line execution
No overload for method 'OutputFile' takes 2 arguments test document
How do i clear my exception?
I want to my code which is to be accept different number of arguments passing.
My Code
class Program
{
static void Main(string[] args)
{
string text = "Sample";
Program obj = new Program();
obj.OutputFile("First text");
obj.OutputFile("Second text {0}",text);
Console.ReadKey();
}
public void OutputFile(string text)
{
string path = "Example.txt";
if (!File.Exists(path))
{
using (TextWriter tw = new StreamWriter(path))
{
tw.WriteLine(text);
Console.WriteLine(text);
}
}
else if (File.Exists(path))
{
using (TextWriter tw = new StreamWriter(path,true))
{
tw.WriteLine(text);
Console.WriteLine(text);
}
}
}
}
I am totally new to this c#. So i hope your answer would be simple.
Your call for OutputFile doesn't seem correct. You have obj.OutputFile("Second text {0}",text); while the method signature is public void OutputFile(string text), which means it requires one parameter.
All you have to do is to change your call to:
obj.OutputFile(string.Format("Second text {0}", text));
And if you are using C# 6 you can make it even better:
obj.OutputFile($"Second text {text}");
change
obj.OutputFile("Second text {0}",text);
to
obj.OutputFile(string.Format("Second text {0}",text));
EDIT:
Your function definition for OutputFile has one parameter. With the comma between the strings you have two parameters instead of the expected one parameter.
I am attempting to alter a series of 4 .bat files. When I run the program, it prompts me for an input and then writes it to the .bat file.
I took the code below from the microsoft documentation on File.Openwrite, then added some variables to point to the files.
As opposed to copy/pasting the code that actually writes the text, I put a for loop around it with the intent of altering the argument so that the File.OpenWrite piece will look to a different variable (and so a different path/directory) during each iteration. I confirmed that the loop works (if I enter one of the path# variables it will iterate through and write to that file 4 times) and that File.OpenWrite is seeing the correct text each iteration. My only guess is that it is looking at the 'path#' argument literally and not seeing it as a variable. Can someone help me understand how I can alter this argument through iteration?
using System;
using System.IO;
using System.Text;
class Test
{
public static void Main()
{
string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string path0 = path + #"\down_fa.bat";
string path1 = path + #"\down_ng.bat";
string path2 = path + #"\down_os.bat";
string path3 = path + #"\down_sp.bat";
string portinput = Console.ReadLine();
string dotbatinput = "DDL -p" + portinput;
// Open the stream and write to it.
for (int i = 0; i < 4; i++)
{
using (FileStream fs = File.OpenWrite("path" + i))
{
Byte[] info =
new UTF8Encoding(true).GetBytes(dotbatinput);
// Add some information to the file.
fs.Write(info, 0, info.Length);
}
}
}
}
You cannot refer to a variable declared in your code using a string and concatenating a number. In this way you pass a literal string to the OpenWrite method not the content of the variable with the name equals to your string.
A simpler approach is to add every batch file to a list of strings and then loop over that list writing the content required
string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
List<string> batFiles = new List<string>();
batFiles.Add(System.IO.Path.Combine(path, "down_fa.bat"));
batFiles.Add(System.IO.Path.Combine(path, "down_ng.bat"));
batFiles.Add(System.IO.Path.Combine(path, "down_os.bat"));
batFiles.Add(System.IO.Path.Combine(path, "down_sp.bat"));
string portinput = Console.ReadLine();
string dotbatinput = "DDL -p" + portinput;
foreach(string batFile in batFiles)
{
using (FileStream fs = File.OpenWrite(batFile))
{
-----
}
}
File.OpenWrite("path" + 0) != File.OpenWrite(path0)
The left side opens a stream to a file called "path0" which you will find in the bin\Debug directory of your project and the right example writes a file at the location specified in the string path0. The same of course applies to the other numbers. A possible solution would be to use an array or a list:
string[] paths = new string[4].Select(x => System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)).ToArray();
string[0] += ...;
string[1] += ...;
string[2] += ...;
string[3] += ...;
foreach (string path in paths)
{
using (FileStream fs = File.OpenWrite(path))
{
// do stuff
}
}
Good day.
I found the example below,
I need to append some more text from another Sub() function.
But i don't know how to.
Could you give me some guide?
THANKS.
using System;
using System.IO;
public class TextToFile
{
private const string FILE_NAME = "MyFile.txt";
public static void Main(String[] args)
{
if (File.Exists(FILE_NAME))
{
Console.WriteLine("{0} already exists.", FILE_NAME);
return;
}
using (StreamWriter sw = File.CreateText(FILE_NAME))
{
sw.WriteLine ("This is my file.");
sw.WriteLine ("I can write ints {0} or floats {1}, and so on.",
1, 4.2);
sw.Close();
}
}
}
If the other function returns the text you want to write, then just write it:
string text = SomeOtherFunction();
sw.Write(text); // or WriteLine to append a newline as well
If you're wanting to append text to an existing file rather than creating a new one, use File.AppendText instead of File.CreateText.
If that's not what you're trying to do, can you clarify the question?
If your function returns a string (or other writable type) you can simply do: sw.WriteLine(theSubINeedToCall());
If you need to process the returned object, you can create a wrapper call and pass the streamWriter to it and then process it i.e:
public void writeOutCustomObject(StreamWriter writer) {
SomeObject theObject = getSomeCustomObject();
writer.WriteLine("ID: " + theObject.ID);
writer.WriteLine("Description: " + theObject.Description);
//.... etc ....
}
add this after Main inside your class
public static void SubFunction(StreamWriter sw)
{
sw.WriteLine("This is more stuff I want to add to the file");
// etc...
}
And then call it in Main like this
using (StreamWriter sw = File.CreateText(FILE_NAME))
{
sw.WriteLine ("This is my file.");
sw.WriteLine ("I can write ints {0} or floats {1}, and so on.", 1,4.2);
MySubFunction(sw); // <-- this is the call
sw.Close();
}