C# File.WriteAllText: How to Write Text to a File - Complete Guide
The `File.WriteAllText` method in C# is used to create a new file and write the specified string content to it, overwriting any existing file with the same name. It has the following syntax:\n\ncsharp\nFile.WriteAllText(string path, string contents);\n\n\nThe `path` parameter is a string that represents the file path to write the content to.\n\nThe `contents` parameter is a string that represents the content to be written to the file.\n\nHere's an example that demonstrates the usage of `File.WriteAllText`:\n\ncsharp\nusing System;\nusing System.IO;\n\nclass Program\n{\n static void Main()\n {\n string path = \"example.txt\";\n string content = \"Hello, World!\";\n \n File.WriteAllText(path, content);\n \n Console.WriteLine(\"Content written to file successfully.\");\n }\n}\n\n\nIn this example, the string "Hello, World!" is written to a file named "example.txt" using the `File.WriteAllText` method. If the file already exists, it will be overwritten. The program then displays a message indicating that the content was written successfully.
原文地址: https://www.cveoy.top/t/topic/pGz0 著作权归作者所有。请勿转载和采集!