日期:2014-05-16  浏览次数:20792 次

为什么找不到文件呢?
在项目中有一个pp.tet文件:


但是为什么下面的代码老是找不到文件呢:

private void Button_Click(object sender, RoutedEventArgs e)
{
    string s1 = "pp.txt";
    if (!File.Exists(s1))  //确定指定的文件是否存在
    {
        MessageBox.Show("文件不存在");
        return;
    }
    using (StreamReader sr = File.OpenText(s1))  
    {
        String input;
        while ((input = sr.ReadLine()) != null)
        {
            MessageBox.Show(input);
        }
        sr.Close();
    }
    Console.ReadKey();
}


哪里写错了呢?
------解决方案--------------------
string s1 = string.Format( "{0}\\pp.txt", Application.StartupPath));
------解决方案--------------------
引用:
在项目中有一个pp.tet文件:


但是为什么下面的代码老是找不到文件呢:

private void Button_Click(object sender, RoutedEventArgs e)
{
    string s1 = "pp.txt";
    if (!File.Exists(s1))  //确定指定的文件是否存在
    {
        MessageBox.Show("文件不存在");
        return;
    }
    using (StreamReader sr = File.OpenText(s1))  
    {
        String input;
        while ((input = sr.ReadLine()) != null)
        {
            MessageBox.Show(input);
        }
        sr.Close();
    }
    Console.ReadKey();
}


哪里写错了呢?


文件的相对位置不对吧
------解决方案--------------------
File.Exists 必须要是全路径。
------解决方案--------------------

把文件路径写全喽....
------解决方案--------------------
在pp.txt上点击右键---》属性------》复制到输出--------》总是复制