Now for this article first we will create a new .NET core console application using c#.net. After creating the console application now, we will create an empty folder. In this folder we will create or place our text file.
In above image you can see there is an empty folder with no file. Inside this folder we will create out text file. Now check the complete code.
Create & Write Text File Using StreamWriter
string folderLocation = "D:\\DemoProject\\DemoFolder";
Console.WriteLine("****Create Text File In a Path****");
//Content To Place in Textfile
string textDate = "This is a example for textfile line 1. This is a example for textfile line 2. This is a example for textfile line 3. This is a example for textfile line 4. ";
Console.WriteLine(textDate);
Console.WriteLine("Enter the file Name: ");
string? textFilwName = Console.ReadLine();
if (!string.IsNullOrEmpty(textFilwName))
{
if (Path.Exists(folderLocation))
{
string filePath = string.Format("{0}{1}{2}{3}", folderLocation, "\\", textFilwName, ".txt");
if (!Path.Exists(filePath))
{
//create file
StreamWriter streamWriter = new StreamWriter(filePath);
//Write date to file
streamWriter.WriteLine(textDate);
//Close the instance
streamWriter.Close();
}
else
{
Console.WriteLine("File alrady exists");
}
}
else
{
Console.WriteLine("Error!!!! Folder pat hdoes not exists.");
}
}
else
{
Console.WriteLine("Error!!!! File name not provided.");
}
Console.ReadLine();
In above code i have declared a variable which is holding the path for the folder location where we are going the create the text file. After that i have declared a variable of string type, and added some sample text, which we will add into the created text file. Now code to ask for user the text file name. One's user added the text file we are validating that the file name should not be blank. If it is not blank on that case, I have validated provided folder path exists or not, if it exists, I have prepared the path for the text file (.txt).
After preparing the file path, validation text file should not exist, after that i have created object for the StreamWriter by passing the path of the file. This piece of code will create a blank text file. In next line i have use streamWriter.WriteLine() to write the data to text file. At the end object have been closed. Below piece is the key part of above code which responsible for creating and writing the text in text file.
//create file
StreamWriter streamWriter = new StreamWriter(filePath);
//Write date to file
streamWriter.WriteLine(textDate);
//Close the instance
streamWriter.Close();
Now let's run the code to check the output.Now enter the file name and press enter. After pressing enter check the folder pat which you have provided to for output text file.
Now let's check the folder.
Open the text file to check the content.
Create & Write Text File Using File.WriteAllText (p1,p2)
Here we will check the next method to create the text using File. This method define under System.IO. Please check the below code.
string folderLocation = "D:\\DemoProject\\DemoFolder";
Console.WriteLine("****Create Text File In a Path****");
//Content To Place in Textfile
string textDate = "File: This is a example for textfile line 1. File: This is a example for textfile line 2. File: This is a example for textfile line 3. File: This is a example for textfile line 4. ";
Console.WriteLine(textDate);
Console.WriteLine("Enter the file Name: ");
string? textFilwName = Console.ReadLine();
if (!string.IsNullOrEmpty(textFilwName))
{
if (Path.Exists(folderLocation))
{
string filePath = string.Format("{0}{1}{2}{3}", folderLocation, "\\", textFilwName, ".txt");
if (!Path.Exists(filePath))
{
//Create and write content to text file
File.WriteAllText(filePath, textDate);
Console.WriteLine("File crated successfully");
}
else
{
Console.WriteLine("File alrady exists");
}
}
else
{
Console.WriteLine("Error!!!! Folder pat hdoes not exists.");
}
}
else
{
Console.WriteLine("Error!!!! File name not provided.");
}
Console.ReadLine();
Here in above code, I have declared a variable for the path of the folder and after that declared a variable to hold the value which we want to add in the text file. After that taken input by user to get the file name. If file name is not blank proceed to check the folder path. If folder path is valid on that case, we concatenate the string values to prepare the path for the file. If file does not exist on that case, I have used File.WriteAllText by providing path of the text file and the file content to create and write the provided content into the file. Please check the below code which is responsible for creating and adding content to the file.
File.WriteAllText(filePath, textDate);
Now let's run the code to check the output. Open the text file to check the content
Not but the least. If we again enter the same file name on that case code will throw error for "File already exists."