System.IO异常未处理

时间:2015-12-16 18:25:29

标签: c# exception system.io.file

我在运行程序时遇到了这个错误:

  

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

     

其他信息:文件名,目录名或卷标语法不正确。

到目前为止,我还无法弄清楚发生了什么。请参阅下面的代码和发生错误的屏幕截图。

Error Occuring

using System;
using System.IO;
using System.Linq;

namespace CipherDecoder
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileText = @"C:\Users\Samuel\Documents\Computer_Science\PaDS\caesarShiftEncodedText.txt\";

            string cipherText = File.ReadAllText(fileText);

            string textPre = "";

            string output = @"C:\\Users\Samuel\Documents\Computer_Science\PaDS\output.txt\";

            char[] cipherChars = new char[691];

            int[] charactersAsInts = File.ReadAllText(fileText).Select(chr => (int)chr).ToArray();

            Console.WriteLine("Process started");

            for(int i = 0; i < charactersAsInts.Length; i++)
            {
                charactersAsInts[i] = charactersAsInts[i] - 5;
            }

            for(int j = 0; j < charactersAsInts.Length; j++)
            {
                if(charactersAsInts[j] == 60)
                {
                    textPre = textPre + "A";
                }
                if (charactersAsInts[j] == 61)
                {
                    textPre = textPre + "B";
                }
                if (charactersAsInts[j] == 62)
                {
                    textPre = textPre + "C";
                }
                if (charactersAsInts[j] == 63)
                {
                    textPre = textPre + "D";
                }
                if (charactersAsInts[j] == 64)
                {
                    textPre = textPre + "E";
                }
                if (charactersAsInts[j] == 65)
                {
                    textPre = textPre + "F";
                }
                if (charactersAsInts[j] == 66)
                {
                    textPre = textPre + "G";
                }
                if (charactersAsInts[j] == 67)
                {
                    textPre = textPre + "H";
                }
                if (charactersAsInts[j] == 68)
                {
                    textPre = textPre + "I";
                }
                if (charactersAsInts[j] == 69)
                {
                    textPre = textPre + "J";
                }
                if (charactersAsInts[j] == 70)
                {
                    textPre = textPre + "K";
                }
                if (charactersAsInts[j] == 71)
                {
                    textPre = textPre + "L";
                }
                if (charactersAsInts[j] == 72)
                {
                    textPre = textPre + "M";
                }
                if (charactersAsInts[j] == 73)
                {
                    textPre = textPre + "N";
                }
                if (charactersAsInts[j] == 74)
                {
                    textPre = textPre + "O";
                }
                if (charactersAsInts[j] == 75)
                {
                    textPre = textPre + "P";
                }
                if (charactersAsInts[j] == 76)
                {
                    textPre = textPre + "Q";
                }
                if (charactersAsInts[j] == 77)
                {
                    textPre = textPre + "R";
                }
                if (charactersAsInts[j] == 78)
                {
                    textPre = textPre + "S";
                }
                if (charactersAsInts[j] == 79)
                {
                    textPre = textPre + "T";
                }
                if (charactersAsInts[j] == 80)
                {
                    textPre = textPre + "U";
                }
                if (charactersAsInts[j] == 81)
                {
                    textPre = textPre + "V";
                }
                if (charactersAsInts[j] == 82)
                {
                    textPre = textPre + "W";
                }
                if (charactersAsInts[j] == 83)
                {
                    textPre = textPre + "X";
                }
                if (charactersAsInts[j] == 84)
                {
                    textPre = textPre + "Y";
                }
                if (charactersAsInts[j] == 85)
                {
                    textPre = textPre + "Z";
                }
                if(charactersAsInts[j] == 27)
                {
                    textPre = textPre + " ";
                }
            }

            Console.WriteLine("{0}", textPre);


        }
    }
}

1 个答案:

答案 0 :(得分:4)

.txt\不是您要使用的文件扩展名。

\放在文件名末尾。

另外,正如KDecker指出的那样,以下行在开头也有一个额外的\

string output = @"C:\\Users\Samuel\Documents\Computer_Science\PaDS\output.txt\";