1

我正在尝试使用我的 Documents 文件夹中的 test.txt 文件来测试这个程序。我很难找到正确的路径。有人可以给我一些想法吗?这是一项家庭作业,我几乎完成了!

using System;
using System.IO;

class Program


{
    // declare constants to use in wind chill factor equation - no magic numbers
    const double EQUATION_NUMBER_ONE = 35.74;
    const double EQUATION_NUMBER_TWO = 0.6215;
    const double EQUATION_NUMBER_THREE = 35.75;
    const double EQUATION_NUMBER_FOUR = 0.4275;
    const double EQUATION_EXPONENT = 0.16;
    const int DEGREE_SYMBOL = 176;

    static void Main()
    {       

        // declare some variables for the main method
        string fileName = "";
        string line = "";
        double t = 0.0;
        double v = 0.0;
        double wchillf = 0.0;
        char degreeSymbol = (char)DEGREE_SYMBOL;


        string environment = System.Environment.GetFolderPath
       (System.Environment.SpecialFolder.Personal) + "c:\\";


        // Give student Info and ask user for a file name.
        // we will read this file and use the info for our input.
        Console.WriteLine("Wind Chill Calculator Braught to you by Teancum");
        Console.WriteLine("for CS 1400 01X");
        Console.WriteLine("11/11/2012");
        Console.WriteLine("Project 8");
        Console.Write("Please enter the file name in your My Documents folder: ");
        fileName = Console.ReadLine();


        string path = environment + fileName;
        //we will create a new instance of the StreamReader class
        //as well find the file in the documents folder
        //this will read the Info and orginise it.
        StreamReader windChillinfo = new StreamReader(path);


        // start the do loop to keep readding the file untill there is no more information
        do
        {
            // read in a line and save it as a string variable
            line = windChillinfo.ReadLine();

            //this if is to make sure there is no invalid info for example if the file is empty.
            if (line != null)
            {
                string[] values = line.Split();
                t = double.Parse(values[0]);
                v = double.Parse(values[1]);

                //here we call the windchillmagic Method
                wchillf = WindChillMagic(t, v);

                //here will be the Results of the windchillmagic method 
                Console.WriteLine("\nFor a temperature {0:f2} F{1}", t, degreeSymbol);
                Console.WriteLine("\nand a wind speed of {0:f2}mph", v);
                Console.WriteLine("\nThe wind chill factor would be = {0:f2}{1}\n", wchillf, degreeSymbol);
            }
        } while (line != null);

        windChillinfo.Close();

        Console.WriteLine("\nThank you for and keep Warm! Press enter to EXIT");

        Console.ReadLine();
    }//End Main()


    static double WindChillMagic(double t, double v)
    {
        double wci = 0.0;
        wci = EQUATION_NUMBER_ONE + (EQUATION_NUMBER_TWO * t) - (EQUATION_NUMBER_THREE * (Math.Pow(v, EQUATION_EXPONENT))) + (EQUATION_NUMBER_FOUR * t * (Math.Pow(v, EQUATION_EXPONENT)));
        return wci;
    }
}//End class Program
4

2 回答 2

3

您如何按照以下方式做一些事情:

String path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "filename.txt");

using (StreamWriter sw = new StreamWriter(path, false))
{
    sw.WriteLine("Hello, file.");
}

这对我有用 - 现在我的 Documents 文件夹中有文件“filename.txt”,文本为“Hello,file”。里面。


当您这样做时,您的版本不起作用:

string environment = System.Environment.GetFolderPath
   (System.Environment.SpecialFolder.Personal) + "c:\\";

这意味着,如果您的个人文件夹是“C:\Users\Username\Documents”,则该environment字符串现在将包含值“C:\Users\Username\Documentsc:\”,并且在将其与

fileName = Console.ReadLine();
string path = environment + fileName;

如果您输入“test.txt”,path现在将包含C:\Users\Username\Documentsc:\test.txt. 您应该使用调试器来查找这些类型的错误。

于 2012-11-12T06:10:59.113 回答
1

这看起来向后:

System.Environment.GetFolderPath
       (System.Environment.SpecialFolder.Personal) + "c:\\";

如果 GetFolderPath 返回“SomeFolder\SomeOtherFolder”,那么您生成的是“SomeFolder\SomeOtherFolderc:\”

如果您在此处放置断点并逐步通过该行,然后将鼠标悬停在environment变量上,您会看到这个问题。

1) 倒退。
2)您可能应该使用 Path.Combine 代替。

string path = environment + fileName;

应该可能使用 Path.Combine 代替,因此在它们之间添加一个斜杠。如果environment不是以斜线结尾怎么办?然后你会得到“C:\SomeFolder\SomeOtherFoldersomeUsersFilename”

于 2012-11-12T06:13:05.000 回答