【问题标题】:C# StreamWriter cannot implicitly convert type to string to SYSTEMS.IOC# StreamWriter 无法将类型隐式转换为字符串到 SYSTEMS.IO
【发布时间】:2016-12-01 14:29:04
【问题描述】:

我需要使用StreamWriter写入/存储用户输入的信息。我已经使用下面的代码完成了这项工作。

我现在需要让它用用户的名字写一个文件,并将信息存储在那里。

隐藏展开复制代码

case 2: //enter new user
    //Enter new user information and add them to file
    Console.Write("Please Enter the number of users you wish to log: ");

    iUserNumber = Convert.ToInt32(Console.ReadLine());

    //open file for writing, in APPEND mode
    using (StreamWriter sw = new StreamWriter("NewUser" +sName,".txt", true))
    {
        //loop for each user
        for (iCount = 1; iCount <= iUserNumber; iCount++)
        {
            // enter details
            Console.Write("Please Enter your Name" + iCount + ": ");
            sName = Console.ReadLine();

            Console.Write("Please Enter the Name of your School: ");
            sSchoolName = Console.ReadLine();
            Console.WriteLine();

            Console.Write("Please Enter the Name of your Class: ");
            sClassName = Console.ReadLine();

            // write to file
            sw.WriteLine(sName);
            Console.WriteLine();
            sw.WriteLine(sSchoolName);
            Console.WriteLine();
            sw.WriteLine(sClassName);

            Console.WriteLine("User data entered: ");
        }
    }

我似乎无法让它工作并不断出现这些错误:

参数 1:不能从 'string' 转换为 'System.IO.Stream' FUNCOCO2

错误 CS0165 使用未分配的局部变量 'sName' FUNCOCO2

错误 CS1503 参数 2:无法从“字符串”转换为 'System.Text.Encoding'

我不知道如何解决这个问题。

我改成这样了:

case 2: //enter new user
    //Enter new user information and add them to file
    Console.Write("Please Enter the number of users you wish to log: ");
    iUserNumber = Convert.ToInt32(Console.ReadLine());
    //enter first name
    Console.WriteLine("Please enter your first Name: ");
    sFirstName = Convert.ToString(Console.ReadLine());

    //open file for writing, in APPEND mode
    using (StreamWriter sw = new StreamWriter(sName))
    {
        //loop for each user
        for (iCount = 1; iCount <= iUserNumber; iCount++)
        {
            // enter details
            Console.Write("Please Enter your Name" + iCount + ": ");
            sLastName = Console.ReadLine();

            Console.Write("Please Enter the Name of your School: ");
            sSchoolName = Console.ReadLine();
            Console.WriteLine();

            Console.Write("Please Enter the Name of your Class: ");
            sClassName = Console.ReadLine();

            // write to file
            sw.WriteLine(sName);
            Console.WriteLine();
            sw.WriteLine(sSchoolName);
            Console.WriteLine();
            sw.WriteLine(sClassName);

            Console.WriteLine("User data entered: ");
        }
    }

这似乎也不起作用。

【问题讨论】:

  • And that doesn't seem to work either.
  • 听起来您想为每个用户创建一个新文件,但您只打开一次文件 - 在循环之外。在您最初的“尝试”中,您还错误地在new StreamWriter("NewUser" +sName,".txt", true) 中使用了逗号而不是字符串连接。由于构造函数没有这样的重载,因此您会遇到一些令人困惑的错误。

标签: c#


【解决方案1】:

按照错误消息(如果您双击它们,它们会指向特定的行号),阅读错误,然后更正错误。


错误 CS0165 使用未分配的局部变量 'sName' FUNCOCO2

在您的流编写器中使用 sName 之前,您永远不会定义和分配它。如果您尝试为每个用户创建 1 个文件,那么您需要更改您的逻辑以反映这一点,并在循环内而不是循环外创建一个新的流编写器。


论据 1:无法从 'string' 转换为 'System.IO.Stream'

new StreamWriter("NewUser" +sName,".txt", true))

应该是

new StreamWriter("NewUser" +sName + ".txt", true))

它接受一个字符串作为第一个参数。


comment 谢谢,我该如何让 StreamReader 打开该文件?

您需要将循环放在流编写器的创建之外。

//loop for each user
for (iCount = 1; iCount <= iUserNumber; iCount++)
{
    Console.Write("Please Enter your Name" + iCount + ": ");
    var sName = Console.ReadLine(); // remove the previous instance you had of sName and make it a scoped variable to the for loop
    using (StreamWriter sw = new StreamWriter("NewUser" +sName + ".txt", true))
    { /*use the streamwriter here */ }
}

【讨论】:

  • 然后sName也必须在之前这一行填写。
  • 谢谢,我如何让 StreamReader 打开该文件?
  • @Igor 谢谢!我现在在将开关与我的变量一起使用时遇到了麻烦,我需要能够通过选项 1-4 使用它们,尽管在“break;”之后我不能重复有没有办法解决这个问题,还是最好对菜单使用 if else 语句?
【解决方案2】:

对于第一部分代码,您尝试在为 sName 分配值之前打开流写入器。 using 关键字只是确保在代码退出该部分后正确处理对象,这意味着在这种情况下您不会到处打开文件。你应该做的是在 for 循环中打开一个文件。正确的代码是:

   case 2: //enter new user
            //Enter new user information and add them to file
            Console.Write("Please Enter the number of users you wish to log: ");
            iUserNumber = Convert.ToInt32(Console.ReadLine());



                //loop for each user
                for (iCount = 1; iCount <= iUserNumber; iCount++)
                {
                    // enter details
                    Console.Write("Please Enter your Name" + iCount + ": ");
                    sName = Console.ReadLine();

                    Console.Write("Please Enter the Name of your School: ");
                    sSchoolName = Console.ReadLine();
                    Console.WriteLine();

                    Console.Write("Please Enter the Name of your Class: ");
                    sClassName = Console.ReadLine();

                    // write to file
                    //open file for writing, in APPEND mode
                    using (StreamWriter sw = new StreamWriter("NewUser" +sName +".txt", true))
                    {
                        sw.WriteLine(sName);
                        Console.WriteLine();
                        sw.WriteLine(sSchoolName);
                        Console.WriteLine();
                        sw.WriteLine(sClassName);
                    }


                    Console.WriteLine("User data entered: ");

                }

            }

编辑:加上,整个 ',' 而不是 '+' 在 streamwriter 的开头。我错过了这一点,以支持显而易见的事情。

【讨论】:

  • 谢谢,我移动了它,现在我收到以下错误: 参数 3: cannot convert from 'string' to 'int', cannot convert string to systems text encoding and cannot convert string to Systems .IO.Stream
  • 检查 iUserNumber 是什么类型。另外,检查 iCount 是什么类型。您没有在给定的 sn-p 中声明 iCount 。大多数情况下,当您创建一个 for 循环时,您会在其中声明一个 int,例如 "for(int iCount = 1; iCount
【解决方案3】:

using (StreamWriter sw = new StreamWriter("NewUser" +sName,".txt", true))

+sName 后面似乎多了一个逗号。该逗号应该是一个 + 来将 3 个字符串部分连接在一起

像这样 using (StreamWriter sw = new StreamWriter("NewUser" + sName + ".txt", true))

Argument 1: cannot convert from 'string' to 'System.IO.Stream' FUNCOCO2

Error CS0165 Use of unassigned local variable 'sName' FUNCOCO2

Error CS1503 Argument 2: cannot convert from 'string' to 'System.Text.Encoding'

错误 1,因为 ""NewUser" +sName" 不是有效的文件路径,因此不能是流

错误 2 因为...不知道,因为我没有看到您声明 sName 的位置

错误 3 因为...额外的逗号正在更改您真正想要使用的构造函数,额外的逗号将“.txt”转换为所需的无效编码。

【讨论】:

    【解决方案4】:

    你可以这样做:

    Console.WriteLine("Please enter your first Name: ");
                String sFirstName = Console.ReadLine();
                String information = String.Empty;
                String append = String.Empty;
    
                while (true)
                {
    
                    Console.Write("Please Enter your Name: ");
                    information = information + Console.ReadLine() + " ";
    
                    Console.Write("Please Enter the Name of your School: ");
                    information = information + Console.ReadLine() + " ";
    
                    if (information.Trim().Equals(""))
                    {
                        break;
                    }
    
                    Console.Write("Please Enter the Name of your Class: ");
                    information = information + Console.ReadLine() + "\t";
    
                    append = append + information;
                    information = String.Empty;
    
    
                }
    
                String fileLocation = "C:...\\New Text Document.txt";
    
    
                using (StreamWriter sw = new StreamWriter(fileLocation))
                {
                    // write to file
                    sw.WriteLine(append);
                    Console.WriteLine("User data entered: ");
    
                }
    

    【讨论】:

    • 您必须按两次 Enter 才能退出
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-13
    • 1970-01-01
    • 2011-09-09
    • 2014-05-15
    相关资源
    最近更新 更多