Skip to main content
 首页 » 编程设计

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

2024年02月20日12JeffreyZhao

我正在尝试使用我的文档文件夹中的 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 

请您参考如下方法:

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

String path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "filename.txt"); 
 
using (StreamWriter sw = new StreamWriter(path, false)) 
{ 
    sw.WriteLine("Hello, file."); 
} 

这对我有用 - 现在我的文档文件夹中有文件“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”,路径 现在将包含 C:\Users\Username\Documentsc:\test.txt。您应该使用调试器来查找这些类型的错误。