简体   繁体   English

C#StreamWriter无法将类型隐式转换为字符串到SYSTEMS.IO

[英]C# StreamWriter cannot implicitly convert type to string to SYSTEMS.IO

I need to use StreamWriter to write/store information that the user is entering. 我需要使用StreamWriter写入/存储用户输入的信息。 I have got this working with the code below. 我已经用下面的代码来工作了。

I now need to make it write a file with the users first name, and store the information there. 现在,我需要使其写入用户名字的文件,并将信息存储在此文件中。

Hide Expand Copy Code 隐藏展开复制代码

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: ");
        }
    }

I can't seem to get this to work and keep coming up with these errors: 我似乎无法使它正常工作并不断提出以下错误:

Argument 1: cannot convert from 'string' to 'System.IO.Stream' FUNCOCO2 参数1:无法从“字符串”转换为“ System.IO.Stream” FUNCOCO2

Error CS0165 Use of unassigned local variable 'sName' FUNCOCO2 错误CS0165使用未分配的局部变量'sName'FUNCOCO2

Error CS1503 Argument 2: cannot convert from 'string' to 'System.Text.Encoding' 错误CS1503参数2:无法从“字符串”转换为“ System.Text.Encoding”

I am not sure how to fix this. 我不确定如何解决此问题。

I changed it to this: 我将其更改为:

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. 而且这似乎也不起作用。

For the first section of code, you are trying to open a streamwriter before you assigned a value to sName. 对于代码的第一部分,您尝试在将值分配给sName之前打开流写入器。 The using keyword just makes sure the object is properly disposed off once the code exits the section, which means you are not leaving files open all over the place in this case. using关键字只是确保一旦代码退出该部分,对象就可以正确放置,这意味着在这种情况下,您不会在所有位置打开文件。 What you should be doing is opening a file within the for loop. 您应该做的是在for循环中打开一个文件。 The correct code would be: 正确的代码是:

   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: ");

                }

            }

Edit: plus, the whole ',' instead of '+' in the opening of the streamwriter. 编辑:加号,整个',',而不是流式编写器开头的'+'。 I missed that in favour of the obvious. 我想念那明显的东西。

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

There seems to be an extra comma right after +sName. + sName后面似乎还有一个逗号。 That comma should be a + to concatenate the 3 string parts together 该逗号应为+,以将3个字符串部分连接在一起

Like such using (StreamWriter sw = new StreamWriter("NewUser" + sName + ".txt", true)) 像这样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'

Error 1 because ""NewUser" +sName" is not a valid file path, therefore cant be a stream 错误1,因为““ NewUser” + sName“不是有效的文件路径,因此不能是流

Error 2 because... dunno since I don't see where you declared sName 错误2因为...不知道,因为我看不到您在哪里声明sName

Error 3 because... the extra comma is changing the constructor you really want to use, that extra comma turns ".txt" into the desired encoding which is invalid. 错误3,因为...多余的逗号正在更改您真正要使用的构造函数,该多余的逗号将“ .txt”转换为所需的无效编码。

Follow the error messages (they point to specific line numbers if you double click on them), read the error, and then correct the mistake. 按照错误消息进行操作(如果双击它们,它们将指向特定的行号),读取错误,然后更正错误。


Error CS0165 Use of unassigned local variable 'sName' FUNCOCO2 错误CS0165使用未分配的局部变量'sName'FUNCOCO2

You never define and assign sName prior to using it in your stream writer. 在流sName中使用sName之前,您永远不会定义和分配它。 If you are trying to create 1 file per user then you need to change your logic to reflect that and create a new streamwriter within the loop instead of outside the loop. 如果要为每个用户创建1个文件,则需要更改逻辑以反映该逻辑,并在循环内而不是循环外创建一个新的streamwriter。


Argument 1: cannot convert from 'string' to 'System.IO.Stream' 参数1:无法从“字符串”转换为“ System.IO.Stream”

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

should be 应该

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

which does accept a string as the 1st parameter. 确实接受字符串作为第一个参数。


comment Thanks, How would I then get StreamReader to then open that file? 评论谢谢,然后我如何让StreamReader打开该文件?

You need to put the loop outside of the creation of the stream writer. 您需要将循环放在流编写器的创建之外。

//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 */ }
}

You can do something like this: 您可以执行以下操作:

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: ");

            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复 StreamWriter 错误“无法将类型字符串隐式转换为 System.IO.StreamWriter” - How to fix StreamWriter error “cannot implicitly convert type string to System.IO.StreamWriter” 不能字符串类型隐式转换对的StreamWriter - Cannot implicitly convert type string to to StreamWriter C#无法将类型&#39;string&#39;隐式转换为&#39;System.IO.StreamReader - C# Cannot implicitly convert type 'string' to 'System.IO.StreamReader C#无法将类型&#39;System.IO.FileInfo&#39;隐式转换为字符串 - C# Cannot Implicitly Convert type 'System.IO.FileInfo' to string 无法在C#soap调用中将类型字符串隐式转换为string [] - Cannot implicitly convert type string to string[] in c# soap call C# - 无法将类型“字符串 []”隐式转换为“字符串” - C# - Cannot implicitly convert type `string[]' to `string' 无法在C#中将类型&#39;String&#39;隐式转换为&#39;string&#39; - Cannot implicitly convert type 'String' to 'string' in C# c# 不能将类型字符串 [] 隐式转换为字符串 - c# cannot implicitly convert type string [] to string C#无法将类型&#39;string&#39;隐式转换为&#39;bool&#39;错误 - C# Cannot implicitly convert type 'string' to 'bool' error C# 控制台应用程序,不能将类型“double”隐式转换为“string”? - C# Console application, cannot implicitly convert type 'double' to 'string'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM