C#PathTooLongException

时间:2016-10-07 13:58:56

标签: c#

我正在为我的C#课工作。我需要使用visual studio 2012创建一个程序,该程序打开并读取文本文件,并报告垃圾词的数量加上邮件是垃圾邮件的概率。我想我已经删除了代码,但是在打开文件时我遇到了异常:

  

未处理的类型' System.IO.PathTooLongException'发生在mscorlib.dll

     

其他信息:指定的路径,文件名或两者都太长。完全限定的文件名必须少于260个字符,目录名必须少于248个字符。

此外,文件名(包含文件本身的文件名)除了文本本身之外还包含路径名C:\ Spam message.txt,它不应该。我试图解决这个问题时,基本上打了一堵砖墙。

这是第一堂课的主要方法

public void ScanMessage(string filename)
    {
        spamCount = 0;
        spamProbability = 0.0;

        StreamReader fileReader;
        FileStream input = new FileStream(filename, FileMode.Open, FileAccess.Read); //This is where the exception is pointing to.
        fileReader = new StreamReader(input);
        messageRead = fileReader.ReadLine();

        while (messageRead != null)
        {
            messageRead = fileReader.ReadLine();
        }
        //Converts message to lowercase
        filename.ToLower();

        //For each word in spam phrases, look through the message. If a spam phrase is found,
        //increment spamCount and add it to the sorteddictionary, or adds an entry to the
        //sortedDictionary if it already exists
        foreach (var word in spamPhrases)
        {
            if (filename.IndexOf(word) != -1)
            {
                spamCount++;
                messageSpamPhrases.Add(word, 1);
            }

           /* else
            {
                ++messageSpamPhrases[word];
            }*/
        }

        spamProbability = spamCount / 30.0 * 100.0;

        //This is for the list of spam phrases found
        phrasesFound = new String[messageSpamPhrases.Count() + 1];
        phrasesFound[0] = string.Format("{0,-22}{1,-12}\n", "Phrase", "Count");
        int x = 1;

        foreach (String key in messageSpamPhrases.Keys)
        {
            phrasesFound[x++] = String.Format("{0,-25} {1,-25}", key, messageSpamPhrases[key]);
        }

        //The message of probability of spam
        spamResult = String.Format("This message has a {0}% chance of being a spam message", spamProbability);

        input.Close();

通过表格中的按钮分析它的方法

private void analyzeBtn(object sender, EventArgs e)
    {
        OpenFileDialog fileChooser = new OpenFileDialog();

        fileChooser.InitialDirectory = "c:\\";

        fileChooser.Filter = "txt files (*.txt)|*.txt";
        fileChooser.FilterIndex = 2;
        fileChooser.RestoreDirectory = true;
        //fileChooser.Multiselect = false;

        DialogResult result = fileChooser.ShowDialog();

        if (result == DialogResult.OK)
        {

            string filename = fileChooser.FileName;

            //result = fileChooser.ShowDialog();
            StreamReader fileReader = new StreamReader(filename);

            while (filename != null)
            {
                textBox1.Text += filename;
                filename = fileReader.ReadLine();
            }
        }
        //textBox1.Text = filename;
        spam.ScanMessage(textBox1.Text);
        textBox1.Show();
        listBox1.BeginUpdate();
        foreach (var element in spam.PhrasesFound)
        {
            listBox1.Items.Add(element);
        }
        listBox1.EndUpdate();

        label2.Text = spam.spamResult;

        listBox1.Show();              
    }

我真正需要的唯一帮助就是弄清楚为什么会发生这种错误。有什么指针吗?

4 个答案:

答案 0 :(得分:0)

  

当路径或文件名长于系统定义的最大长度时引发的异常。

-

  

在Windows API中(以下段落中讨论了一些例外),路径的最大长度为MAX_PATH,定义为260个字符。本地路径按以下顺序构成:驱动器号,冒号,反斜杠,由反斜杠分隔的组件以及终止空字符。

-

  

如果您的代码没有PathDiscovery权限,则此异常的错误消息可能只包含文件或目录名称而不是完整路径。

-

  

完整路径不得超过260个字符,以保持与Windows操作系统的兼容性。有关此限制的详细信息,请参阅BCL团队博客中的条目Long Paths in .NET

答案 1 :(得分:0)

罪魁祸首在这里:

您正在将正在阅读的文本添加到文件名中,ergo。文件名将会很大(除了它可能包含无效字符)

   if (result == DialogResult.OK)
            {

                string filename = fileChooser.FileName;

                //result = fileChooser.ShowDialog();
                StreamReader fileReader = new StreamReader(filename);

                while (filename != null)
                {
                    textBox1.Text += filename;
                    filename = fileReader.ReadLine(); // <-- THIS IS THE PROBLEM
                }
            }

我想你想这样做:

   if (result == DialogResult.OK)
            {

                string filename = fileChooser.FileName;

                StreamReader fileReader = new StreamReader(filename);

               string line;
               while ((line = reader.ReadLine()) != null)
               {
                    textBox1.Text += line+"\r\n";
               }
            }

答案 2 :(得分:0)

在这篇文章中:

        while (filename != null)
        {
            textBox1.Text += filename;
            filename = fileReader.ReadLine();
        }
    }
    //textBox1.Text = filename;
    spam.ScanMessage(textBox1.Text);

由于textBox1.Text+=的值会填充文件中的所有内容。这可能是数百/数千个字符。

然后,你做spam.ScanMessage(textBox1.Text)(可能包含数千个字符) 在ScanMessage方法中,您尝试在第4个语句中打开具有该路径的文件。这溢出了。

帮自己一个忙:学习如何调试。不要苛刻或刺​​痛,但这会让你的生活更轻松。

答案 3 :(得分:0)

好的,根据你的最新评论,我已经给了这个镜头:

DialogResult result = fileChooser.ShowDialog();

if (result == DialogResult.OK)
{
    string filename = fileChooser.FileName;
    spam.ScanMessage(filename);
    //...
}

public void ScanMessage(string filename)
{
    spamCount = 0;
    spamProbability = 0.0;

    var messageRead="";
    using(var input = new StreamReader(filename))
    {
        messageRead = input.ReadToEnd();
    }
    //Converts message to lowercase
    messageRead = messageRead.ToLower();

    //For each word in spam phrases, look through the message. If a spam phrase is found,
    //increment spamCount and add it to the sorteddictionary, or adds an entry to the
    //sortedDictionary if it already exists
    foreach (var word in spamPhrases)
    {
        if (messageRead.Contains(word))
        {
            spamCount++;
            messageSpamPhrases.Add(word, 1);
        }
    }

    spamProbability = spamCount / 30.0 * 100.0;

    //This is for the list of spam phrases found
    phrasesFound = new String[messageSpamPhrases.Count() + 1];
    phrasesFound[0] = string.Format("{0,-22}{1,-12}\n", "Phrase", "Count");
    int x = 1;

    foreach (String key in messageSpamPhrases.Keys)
    {
        phrasesFound[x++] = String.Format("{0,-25} {1,-25}", key, messageSpamPhrases[key]);
    }

    //The message of probability of spam
    spamResult = String.Format("This message has a {0}% chance of being a spam message", spamProbability);
}

这可能仍然没有完全踢它,因为你的部分代码丢失了。它会让你走得更远。