如何将在控制台中编辑过的数据保存到文件中

时间:2013-11-11 22:34:23

标签: c# file-io

我有一个需要编辑的原始文件,我已设法打开此文件并使用代码来纠正我已被要求通过将原始文件更改为字符串已完成的问题,现在我需要保存这些更改,如何保存到新文件控制台上显示的内容?我尝试过使用流式编写器,但不知道如何保存已编辑的字符串。

2 个答案:

答案 0 :(得分:0)

基于新/详细要求的新答案: 我修改了你的代码并添加了一些新行。

string path = @"c:\temp\MyIOFile.txt";

            try
            {

                string file = File.ReadAllText(path);
                //The code wrote to the right hand side finds the file listed from my C drive

                string longstr = file;

                string[] strs = longstr.Split(':', '*');

                foreach (string ss in strs)
                {
                    Console.WriteLine(ss);
                }

                //before text is written, you say you want to modify it
                string newText = "*enter new file contents here*";

                //you can add new text (Append) or
                //change all the contents of the file
                //set the value of whatToDo to "Append" to add new text to the file
                //set the value of whatToDo to any value other than "Append" to replace
                //the entire contents of the filw with the data in variable newText
                string whatToDo = "Append";

                if (whatToDo == "Append")
                {
                    //append to existing text
                    //variable file contains old text
                    //varaible newText contains the new text to be appended
                    File.AppendAllText(path, newText);
                }
                else
                {
                    //creates new contents in the file.
                    //varaiable new text contains the new text representing
                    //file contents
                    File.WriteAllText(path, newText);
                }


                //string file = File.AppendAllText(@"C:\Users\path\.......");

            }
            catch (Exception ex)
            {
                Console.WriteLine("*** Error:" + ex.Message);
            }
            Console.WriteLine("*** Press Enter key to exit");
            Console.ReadLine();
        }

原始答案

可能会有所帮助:

    string path = @"c:\temp\MyIOFile.txt";
    if (!File.Exists(path))
    {
        // File does not exist - What do you want to do? 
    }
    try
    {
    // Open the file to read from and store result in a string variable
    string readText = File.ReadAllText(path);

    // modify the text somehow before appending to file
    string appendText =readText+ Environment.NewLine+ "This is extra text";
    File.AppendAllText(path, appendText, Encoding.UTF8);
    }
    catch (Exception ex)
    {
        Console.WriteLine ("***Error:" + ex.Message);
        // display errors
    }

答案 1 :(得分:0)

string file = File.ReadAllText(@"C:\Users\path.......");
        //The code wrote to the right hand side finds the file listed from my C drive

 string longstr = file;

string[] strs = longstr.Split(':', '*');

foreach (string ss in strs)
        {
            Console.WriteLine(ss);
        }
string file = File.AppendAllText(@"C:\Users\path\.......");
        Console.ReadLine();