C#中的文件路径问题(主页

时间:2012-11-12 06:03:47

标签: c#

我正在尝试使用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

2 个答案:

答案 0 :(得分: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。您应该使用调试器来查找这些类型的错误。

答案 1 :(得分: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”