0

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

4

2 回答 2

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
    }
于 2013-11-11T22:58:16.123 回答
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();
于 2013-11-12T15:18:23.540 回答