Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将在控制台中编辑的数据保存到文件中_C#_File Io - Fatal编程技术网

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

C# 如何将在控制台中编辑的数据保存到文件中,c#,file-io,C#,File Io,我有一个需要编辑的原始文件,我已经设法打开了这个文件,并使用代码纠正了我被要求解决的问题。这是通过将原始文件更改为字符串来完成的,现在我需要保存这些更改,如何将控制台上显示的内容保存到新文件?我尝试过使用stream writer,但不知道如何保存编辑过的字符串。基于新的/详细要求的新答案: 我修改了您的代码并添加了一些新行 string path = @"c:\temp\MyIOFile.txt"; try {

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

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

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
    }

那么现在您想将字符串保存到文件中吗?文件已被打开成为字符串,所以我想是这样。这个问题可能是您的解决方案:(问题,而不是答案)它不起作用,当我尝试它时,我收到一条错误消息,说“writealltext”没有重载方法。这没有帮助,插入我编写的代码会有帮助吗?是的。另外,告诉我们你到底想要什么,你的问题是什么。好的,我有一个原始文件,里面有一块文本,我必须分开,这样做我已经将文件转换成字符串,这样我就可以使用字符串。拆分我的程序,我已经完成了这部分,现在我想将编辑的文本保存到一个新文件中。这是将文件转换为字符串的代码。字符串file=file.ReadAllText(@“C:\Users\path…”);我假设您必须对在名为“file”的变量中读取的字符串进行一些更改,因此您进行了更改,并使用上述代码中的AppendAllText方法将结果字符串保存到文件中。我把来自读取操作的文本称为“readText”,而不是“file”——仅此而已。我发布了一个新的答案。希望这有帮助。
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();