在c#中打开文件路径
本文关键字:文件 路径 | 更新日期: 2023-09-27 18:08:01
我在winform打开绘图时遇到了麻烦。我得到的错误说NullReferenceException未处理,并突出显示路径。任何帮助都是感激的。由于
private void button2_Click(object sender, EventArgs e)
{
//Open Solidworks Drawing
ModelDoc2 swModel = default(ModelDoc2);
DocumentSpecification swDocSpecification = default(DocumentSpecification);
string sName = null;
long longstatus = 0;
long longwarnings = 0;
// Drawing document path and name
swDocSpecification = (DocumentSpecification)swApp.GetOpenDocSpec("C:''location''????.slddrw");//File Location
sName = swDocSpecification.FileName;
// Sheet name
swDocSpecification.SheetName = "BOM"; //Open to the BOM sheet
swDocSpecification.DocumentType = (int)swDocumentTypes_e.swDocDRAWING;
swDocSpecification.ReadOnly = true;
swDocSpecification.Silent = false;
// Open the specified sheet in the specified drawing document
swModel = swApp.OpenDoc7(swDocSpecification);
longstatus = swDocSpecification.Error;
longwarnings = swDocSpecification.Warning;
}
System.Diagnostics.Process.Start("explorer.exe c:''");
有两种可能性,为什么你得到NullReferenceException
- swApp是空的,调用包括
GetOpenDocSpec
在内的任何东西都不起作用 - GetOpenDocSpec里面的东西不是按照它应该的方式写的,它没有做正确的检查。它抛出一个空异常
使用调试器检查swApp == null应该很容易。使用auto或watch窗口,将鼠标悬停在变量上,命令窗口中的?swApp == null等