构建解决问题

时间:2011-08-30 17:51:48

标签: c# visual-studio-2010 debugging try-catch

我正在使用带有C#的Visual Studio 2010。

当我使用开始调试工具(F5)时,一切似乎都能正常工作。所以我构建了解决方案。

现在我关闭Visual Studio 2010并转到'应用程序'为该计划保留。当我双击文件时,它运行程序正常。但是,当我去点击" save" GUI中的按钮可以保存文件但是被我的try / catch块捕获。

为什么双击它时会在应用程序中发生这种情况,而不是在我运行Visual Studio 2010进行调试时?

修改

try
{
    StreamWriter sw2 = new StreamWriter(saveFile2.FileName);
    List<string> universalLineList = new List<string>();
    string file = saveFile2.FileName;
    string[] splitFile = file.Split('\\');
    string[] savedName = splitfile[6].Split('.');
    int i = 1;

    sw2.WriteLine(savedName[0]);
    sw2.WriteLine("Created on: " + DateTime.Now.ToShortDateString() + " at: " + DateTime.Now.ToString("HH:mm:ss"));
    sw2.WriteLine();

    while (i < stepAndRepeatcounter)
    {
        sw2.WriteLine("Line: {0}", i);
        i++;
    }

    sw.WriteLine();

    foreach(var line in theUniversalList)
    {
        if (line.Name.ToUpper().Contains("FID"))
        {
            sw2.WriteLine("Fiducial: {0}, X: {1}, Y: {2}",
                          line.Name,
                          line.XPlacement,
                          line.YPlacement);
        }
    }

    sw2.WriteLine("Placements: ");

    foreach (var line in theUniversalList)
    {
        if (!line.Name.ToUpper().Contains("FID"))
        {
            sw2.WriteLine("Name: {0}, X: {1}, Y: {2}",
                          line.Name,
                          line.XPlacement,
                          line.YPlacement);
        }
    }

    int j = 0;
    sw2.WriteLine("Slots: ");
    foreach (var line in theUniversalList)
    {
        if (!line.Name.ToUpper().Contains("FID"))
        {
            sw2.WriteLine("Slot: {0}, P/D: {1}, P/N: {2}",
                          j,
                          line.PartDescription,
                          line.PartNumber);

            sw2.WriteLine("Slot: 22, P/D: Reject, P/N: {0}",
                          line.PartNumber);

            j++;
        }
    }

    sw2.Close();
}

catch (Exception)
{
    MessageBox.Show("Could not format and save Universal-1 Placements", "Universal-1 Save Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}

1 个答案:

答案 0 :(得分:1)

我认为这是以下几行:

string file = saveFile2.FileName;
string[] splitFile = file.Split('\\');
string[] savedName = splitfile[6].Split('.');

我猜你的路径对于第7个元素来说不够长,当你从命令行运行文件时(可能路径就像“。\ xzy.txt”

要100%确定也检查异常的StackTrace,以获得错误的行。