各位大佬,
小白能力有限,没搞懂VS环境下调试和直接运行exe文件到底区别在哪儿,卡在这一个多星期了,还请大佬们帮忙看看,不甚感激。
问题描述:通过C# UIA的方法编写了一个Winform程序,其中一部分功能是通过编写的程序调用电脑上另一个软件的UI控件,实现自动化点击按钮选择文件输入等功能。在VS2019环境下点击start调试时没有报错,但是从debug文件夹把exe文件单独拿出来运行时发现程序报错(偶尔运气好的话也不会报错)。通过在winform界面增加文本输出发现是在查找电脑上被调用的程序的窗口界面时没找到就报错。
报错代码段:
private void GetEcuInfo(String EcuAddress)
{
AutomationElement formDPS = null;
//AutomationElement formProgramming = null;
AutomationElement formSelectJ2534 = null;
AutomationElement formGCIErrorTip = null;
AutomationElement NoVINTip = null;
AutomationElement formSecurityFacility = null;
AutomationElement formEcuInfo = null;
string successTip = null;
AutomationElement root = AutomationElement.RootElement;
AutomationElementCollection coll = root.FindAll(TreeScope.Children, new
PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window));
txt_Status.AppendText("Start find DPS window" + "\r\n");(exe文件运行时输出Start find DPS Window 后就报错了)
for (int numWait = 0; formDPS == null && numWait < 500; numWait++)
{
foreach (AutomationElement i in coll)
{
string name1 = (string)(i.GetCurrentPropertyValue(AutomationElement.NameProperty));
if (name1.Contains("Development Programming System"))
//由于打开子窗口后第一层窗口的名称会“Development Programming System-Programming”,因此此处通过命名包含关系查找
{
formDPS = i;
Console.WriteLine(formDPS.GetCurrentPropertyValue(AutomationElement.NameProperty) + "for GCI");
}
}
}
exe文件运行报错截图如下所示:
上图报错的详细信息如下:
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.Windows.Automation.ElementNotAvailableException: The target element corresponds to UI that is no longer available (for example, the parent window has closed).
at MS.Internal.AutomationProxies.Misc.ThrowWin32ExceptionsIfError(Int32 errorCode)
at MS.Internal.AutomationProxies.Misc.GetClassName(IntPtr hwnd)
at MS.Internal.AutomationProxies.WindowsFormsHelper.IsWindowsFormsControl(IntPtr hwnd, FormControlState& state)
at MS.Internal.AutomationProxies.ProxyHwnd.GetElementProperty(AutomationProperty idProp)
at MS.Internal.AutomationProxies.ProxySimple.System.Windows.Automation.Provider.IRawElementProviderSimple.GetPropertyValue(Int32 propertyId)
at MS.Internal.Automation.UiaCoreApi.CheckError(Int32 hr)
at System.Windows.Automation.AutomationElement.GetCurrentPropertyValue(AutomationProperty property, Boolean ignoreDefaultValue)
at System.Windows.Automation.AutomationElement.GetCurrentPropertyValue(AutomationProperty property)
at Panel.Pannel.GetEcuInfo(String EcuAddress)
at Panel.Pannel.Programming()
at Panel.Pannel.<btnStart_Click>d__47.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.<>c.<ThrowAsync>b__6_0(Object state)
************** Loaded Assemblies **************
mscorlib
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4180.0 built by: NET48REL1LAST_B
CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
Panel
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///D:/sgmuserprofile/s9b5ga/Desktop/Panel.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4180.0 built by: NET48REL1LAST_B
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4001.0 built by: NET48REL1LAST_C
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
Assembly Version: 4.0.0.0
Win32 Version: 4.8.3761.0 built by: NET48REL1
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
Assembly Version: 4.0.0.0
Win32 Version: 4.8.3761.0 built by: NET48REL1
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Core
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4180.0 built by: NET48REL1LAST_B
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
System.Xml
Assembly Version: 4.0.0.0
Win32 Version: 4.8.3761.0 built by: NET48REL1
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
Accessibility
Assembly Version: 4.0.0.0
Win32 Version: 4.8.3761.0 built by: NET48REL1
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Accessibility/v4.0_4.0.0.0__b03f5f7f11d50a3a/Accessibility.dll
----------------------------------------
UIAutomationClient
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4180.0 built by: NET48REL1LAST_B
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/UIAutomationClient/v4.0_4.0.0.0__31bf3856ad364e35/UIAutomationClient.dll
----------------------------------------
UIAutomationTypes
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4180.0 built by: NET48REL1LAST_B
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/UIAutomationTypes/v4.0_4.0.0.0__31bf3856ad364e35/UIAutomationTypes.dll
----------------------------------------
UIAutomationProvider
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4180.0 built by: NET48REL1LAST_B
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/UIAutomationProvider/v4.0_4.0.0.0__31bf3856ad364e35/UIAutomationProvider.dll
----------------------------------------
UIAutomationClientsideProviders
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4180.0 built by: NET48REL1LAST_B
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/UIAutomationClientsideProviders/v4.0_4.0.0.0__31bf3856ad364e35/UIAutomationClientsideProviders.dll
----------------------------------------
WindowsBase
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4180.0 built by: NET48REL1LAST_B
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/WindowsBase/v4.0_4.0.0.0__31bf3856ad364e35/WindowsBase.dll
----------------------------------------
************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.
For example:
<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>
When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.
报错代码段
private void GetEcuInfo(String EcuAddress)
{
txt_Status.AppendText("Start GetEcuInfo" + "\r\n");
AutomationElement formDPS = null;
//AutomationElement formProgramming = null;
AutomationElement formSelectJ2534 = null;
AutomationElement formGCIErrorTip = null;
AutomationElement NoVINTip = null;
AutomationElement formSecurityFacility = null;
AutomationElement formEcuInfo = null;
string successTip = null;
txt_Status.AppendText("Initialize" + "\r\n");
AutomationElement root = AutomationElement.RootElement;
txt_Status.AppendText("Get desktop" + "\r\n");
AutomationElementCollection coll = root.FindAll(TreeScope.Children, new
PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window));
txt_Status.AppendText("Start find DPS window" + "\r\n");
for (int numWait = 0; formDPS == null && numWait < 500; numWait++)
{
foreach (AutomationElement i in coll)
{
string name1 = (string)(i.GetCurrentPropertyValue(AutomationElement.NameProperty));
if (name1.Contains("Development Programming System"))
{
formDPS = i;
Console.WriteLine(formDPS.GetCurrentPropertyValue(AutomationElement.NameProperty) + "for GCI");
}
}
}