gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
Write a C# program to create a text file and write the following Text in it, then open…
- User
Write a C# program to create a text file and write the following Text in it, then open the file automatically with coding (Mimicking double-clicking on it)
- Assistant
Sure! Here's a C# program that creates a text file, writes the requested text into it, and opens the file automatically using the default associated program (mimicking double-clicking on it):
using System; using System.IO; using System.Diagnostics; class Program { static void Main() { string filePath = @"C:\textfile.txt"; // Write the text into the file using (StreamWriter sw = File.CreateText(filePath)) { sw.WriteLine("Hello, this is my text file!"); sw.WriteLine("It contains some sample text."); sw.WriteLine("Hope you find it useful!"); } // Open the file automatically ProcessStartInfo psi = new ProcessStartInfo(filePath); psi.UseShellExecute = true; Process.Start(psi); } }In this program, replace
"C:\textfile.txt"with the desired file path where you want to create the text file.When you run this program, it will create a text file at the specified file path, write the provided text into it, and then open the file automatically with the default associated program on your computer.