【问题标题】:Length cannot be less than zero Parameter Name: length长度不能小于零 参数名称:length
【发布时间】:2026-01-03 19:30:01
【问题描述】:

我一直在寻找解决这个错误的方法。在 VS2015 的调试和发布模式下,我没有收到任何错误,并且代码会生成所需的结果。一旦程序被编译和安装,当按下电子邮件按钮生成代码时,应用程序就像它开始工作一样,然后在大约 20 秒后给出错误“长度不能小于零参数名称:长度”但是应用程序继续,但未完成相关代码。

我已禁用并尝试引用长度的一行代码,这没有任何区别。

我对可能导致这种情况的原因有限,所以我正在寻求帮助以在大海捞针。

下面是有问题的代码:

try
        {
            // Create the Outlook application.
            Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application();
            // Create a new mail item.
            Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
            // Set HTMLBody. 
            //add the body of the email
            oMsg.HTMLBody = DictMailParam["Body"].ToString();
            //Add an attachment.
            String sDisplayName = DictMailParam["AttachmentName"].ToString();
            int iPosition = (int)oMsg.Body.Length + 1;
            int iAttachType = (int)Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue;
            //now attached the file
            //Microsoft.Office.Interop.Outlook.Attachment oAttach = oMsg.Attachments.Add
            //                             (@"C:\\fileName.jpg", iAttachType, iPosition, sDisplayName);

            foreach (string strFile in DictMailParam["Attachments"].ToString().Split(',').ToList())
            {
                Microsoft.Office.Interop.Outlook.Attachment oAttach = oMsg.Attachments.Add
                        (strFile, iAttachType, iPosition, sDisplayName);
            }
            //Subject line
            oMsg.Subject = DictMailParam["Subject"].ToString();
            // Add a recipient.
            Microsoft.Office.Interop.Outlook.Recipients oRecips = (Microsoft.Office.Interop.Outlook.Recipients)oMsg.Recipients;
            Microsoft.Office.Interop.Outlook.Recipient oRecip = null; 
            // Change the recipient in the next line if necessary.
            foreach (var MailId in DictMailParam["ToAddress"].ToString().Split(';').ToArray())
            {
                if (!string.IsNullOrEmpty(MailId))
                {
                    oRecip = (Microsoft.Office.Interop.Outlook.Recipient)oRecips.Add(MailId);//DictMailParam["ToAddress"].ToString()
                    oRecip.Resolve();
                    // Send.
                }

            }

            oMsg.Send();
            MessageBox.Show("Purchase Order has been sent to your email." + "\n" + "Please check your mail.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            // Clean up.
            oRecip = null;
            oRecips = null;
            oMsg = null;
            oApp = null;
        }//end of try block
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }//end of catch
    }//end of Email Method

【问题讨论】:

  • 哪一行会抛出异常?
  • 尝试从异常中写出你的堆栈跟踪。我看不出有任何明显错误,但堆栈跟踪会提供更多信息。
  • 正如 Ben 所说,也写出你的堆栈跟踪(ex.ToString(),而不是 ex.Message)。
  • 大家好,我都试过了,但它仍然给我同样的错误,没有额外的信息。关于下一步尝试解决此问题的任何想法?

标签: c# smartsheet-c#-sdk-v2


【解决方案1】:

将捕获代码更改为 MessageBox.Show(ex.ToString()...) 而不是 ex.Message。这样,当发生错误时,您将收到一条消息,说明哪一行正在引发异常。然后回到这里,告诉它是哪条线。一旦我们知道问题所在,解决您的问题就会容易得多。

【讨论】:

  • 该代码不应该修复错误。该代码应该告诉我们错误在哪里。你能在这里发布你得到的确切错误文本吗?
  • 我得到“长度不能小于零参数名称:长度”,并带有选择确定按钮的选项。
  • 我试图在代码的间隔放置消息框,认为这可能会破坏部分,但它不会更改为错误消息。有两个具有相似代码的按钮,一个打印预览(没有错误),另一个不工作并给出错误。我在想对代码的调用可能是错误的 this.CreateReport(0);因为打印预览选项是 1,而电子邮件是 0。在创建报告的代码的第一行中,它说 private void CreateReport(int isPreview = 1) 这可能是问题吗?
最近更新 更多