无法将类型System.IO.Streamreader转换为double

时间:2013-11-28 20:24:03

标签: c#

我必须从系统加载一个文本文件,我收到以下错误:

  

无法将类型System.IO.Streamreader转换为double

class Program
{
    static void Main(string[] args)
    {
        Double num1;
        int  num2;
        Double  mul;
        Double div;
        Double result;
        Double conv;

        // this we are adding m3 size using int num1
        Console.WriteLine("What is M3 Size");
        num1 = Convert.ToDouble(Console.ReadLine());

        // now we are adding buying price using number 2
        Console.WriteLine("What is buying price");
        num2 = Convert.ToInt32(Console.ReadLine());

        System.IO.StreamReader file = new System.IO.StreamReader("c:\\conversion.txt");
        conv = new System.IO.StreamReader("c:\\conversion.txt");

        div = num2 / conv;
        mul = num1 * 128;
        result = div + mul;

        Console.WriteLine(" \n\nC&F $ {0}", result);

        Console.ReadLine();
        } 
    }
}

2 个答案:

答案 0 :(得分:2)

streamreader只是读者,你实际上需要.Read()来获取文件

using (TextReader reader = new StreamReader("C:\blabla.txt"))
{
    //Create a reader - the using ensures that the system cleans up when we're done.

    //Read the whole file as a single string
    string fileContents = reader.ReadToEnd();

    //Parse the string to a double
    double conv= Double.Parse(fileContents);

 }

答案 1 :(得分:0)

您将变量conv声明为double,然后尝试为其分配StreamReader

阅读提供的MSDN文章中如何使用StreamReader