C# Winform编程 Debug文件夹中的exe文件单独执行时报错,但是VS环境下代码调试时不报错

Helloworld3020 2020-12-22 05:00:41
各位大佬,

小白能力有限,没搞懂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");
}
}
}

...全文
15805 21 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 21 楼 Helloworld3020 的回复:
[quote=引用 19 楼 堆代码的爱因斯坦 的回复:]在项目代码引用的那里,把所有的dll的生成操作 改为 复制到本地,目测是相关的dll没有正确的被引用
嗯,这一步做了,只是复制到本地后,我又把exe文件单独复制到了桌面再运行的[/quote]你运行程序不应该把exe放置到文件夹执行吗?干嘛单独复制?你可以发送到桌面快捷方式运行啊。
Helloworld3020 2021-01-13
  • 打赏
  • 举报
回复
引用 19 楼 堆代码的爱因斯坦 的回复:
在项目代码引用的那里,把所有的dll的生成操作 改为 复制到本地,目测是相关的dll没有正确的被引用
嗯,这一步做了,只是复制到本地后,我又把exe文件单独复制到了桌面再运行的
Helloworld3020 2021-01-13
  • 打赏
  • 举报
回复
引用 16 楼 Cpx_CsdnLt 的回复:
[quote=引用 14 楼 Helloworld3020 的回复:][quote=引用 12 楼 Cpx_CsdnLt 的回复:][quote=引用 10 楼 Helloworld3020 的回复:][quote=引用 9 楼 Cpx_CsdnLt 的回复:]你引用的是不是有第三方的dll没有复制到本地啊
没太理解你说的引用指的什么,我用的是同一个电脑跑的,而且有时候是可以正常运行的[/quote]引用第三方的东西,比如DevExpress的控件,他的dll文件你需要看下属性 里面属性有个选项忘了叫啥了 改成复制到本地 相当于你重新生成项目会生成在你debug或者其他目录下 你单独拿出来就不会报错[/quote] 改成复制到本地后的确是把dll复制到了debug目录下,但如果我把debug中的exe再复制到桌面后运行时还是会出现这个问题[/quote]......你单独复制个exe出来肯定运行不了啊兄弟,你给debug文件全复制出来啊。。。。。。。。。。[/quote] 好吧,刚入门,没有这种sense。这样的话那岂不是只能将文件夹内其他dll一起打包后释放给到大家才能用了?其实我一开始的想法是将生成的exe文件单独作为一个免安装工具释放给大家使用的。
  • 打赏
  • 举报
回复
在项目代码引用的那里,把所有的dll的生成操作 改为 复制到本地,目测是相关的dll没有正确的被引用
良朋 2021-01-08
  • 打赏
  • 举报
回复
总不会放到release里面去了吧?
Helloworld3020 2021-01-07
  • 打赏
  • 举报
回复
引用 13 楼 Water Lee 的回复:
我也觉得是你引用的DLL没有复制到本地,这样在VS中运行时可以找到,但直接运行会找不到。
试了,效果不理想
Helloworld3020 2021-01-07
  • 打赏
  • 举报
回复
引用 12 楼 Cpx_CsdnLt 的回复:
[quote=引用 10 楼 Helloworld3020 的回复:][quote=引用 9 楼 Cpx_CsdnLt 的回复:]你引用的是不是有第三方的dll没有复制到本地啊
没太理解你说的引用指的什么,我用的是同一个电脑跑的,而且有时候是可以正常运行的[/quote]引用第三方的东西,比如DevExpress的控件,他的dll文件你需要看下属性 里面属性有个选项忘了叫啥了 改成复制到本地 相当于你重新生成项目会生成在你debug或者其他目录下 你单独拿出来就不会报错[/quote] 改成复制到本地后的确是把dll复制到了debug目录下,但如果我把debug中的exe再复制到桌面后运行时还是会出现这个问题
Cpx_CsdnLt 2021-01-07
  • 打赏
  • 举报
回复
引用 14 楼 Helloworld3020 的回复:
[quote=引用 12 楼 Cpx_CsdnLt 的回复:][quote=引用 10 楼 Helloworld3020 的回复:][quote=引用 9 楼 Cpx_CsdnLt 的回复:]你引用的是不是有第三方的dll没有复制到本地啊
没太理解你说的引用指的什么,我用的是同一个电脑跑的,而且有时候是可以正常运行的[/quote]引用第三方的东西,比如DevExpress的控件,他的dll文件你需要看下属性 里面属性有个选项忘了叫啥了 改成复制到本地 相当于你重新生成项目会生成在你debug或者其他目录下 你单独拿出来就不会报错[/quote] 改成复制到本地后的确是把dll复制到了debug目录下,但如果我把debug中的exe再复制到桌面后运行时还是会出现这个问题[/quote]你如果重新生成的项目是debug模式,你就把整个debug文件拿出来
Cpx_CsdnLt 2021-01-07
  • 打赏
  • 举报
回复
引用 14 楼 Helloworld3020 的回复:
[quote=引用 12 楼 Cpx_CsdnLt 的回复:][quote=引用 10 楼 Helloworld3020 的回复:][quote=引用 9 楼 Cpx_CsdnLt 的回复:]你引用的是不是有第三方的dll没有复制到本地啊
没太理解你说的引用指的什么,我用的是同一个电脑跑的,而且有时候是可以正常运行的[/quote]引用第三方的东西,比如DevExpress的控件,他的dll文件你需要看下属性 里面属性有个选项忘了叫啥了 改成复制到本地 相当于你重新生成项目会生成在你debug或者其他目录下 你单独拿出来就不会报错[/quote] 改成复制到本地后的确是把dll复制到了debug目录下,但如果我把debug中的exe再复制到桌面后运行时还是会出现这个问题[/quote]......你单独复制个exe出来肯定运行不了啊兄弟,你给debug文件全复制出来啊。。。。。。。。。。
Helloworld3020 2020-12-28
  • 打赏
  • 举报
回复
引用 4 楼 dugupiaoyun 的回复:
应该是你的系统的问题,将修改生成的平台改为x86,而不是any cpu。然后重新编译,再试试单独运行Debug里的exe吧!
谢答,不是这个问题,我试了,我电脑上只有选any cpu才能运行起来。
Helloworld3020 2020-12-28
  • 打赏
  • 举报
回复
引用 9 楼 Cpx_CsdnLt 的回复:
你引用的是不是有第三方的dll没有复制到本地啊
没太理解你说的引用指的什么,我用的是同一个电脑跑的,而且有时候是可以正常运行的
Water Lee 2020-12-28
  • 打赏
  • 举报
回复
我也觉得是你引用的DLL没有复制到本地,这样在VS中运行时可以找到,但直接运行会找不到。
Cpx_CsdnLt 2020-12-28
  • 打赏
  • 举报
回复
引用 10 楼 Helloworld3020 的回复:
[quote=引用 9 楼 Cpx_CsdnLt 的回复:]你引用的是不是有第三方的dll没有复制到本地啊
没太理解你说的引用指的什么,我用的是同一个电脑跑的,而且有时候是可以正常运行的[/quote]引用第三方的东西,比如DevExpress的控件,他的dll文件你需要看下属性 里面属性有个选项忘了叫啥了 改成复制到本地 相当于你重新生成项目会生成在你debug或者其他目录下 你单独拿出来就不会报错
Helloworld3020 2020-12-25
  • 打赏
  • 举报
回复
引用
显然引用 的东西被你删除了。DEBUG全部拿走就可以了
什么意思,我没有动任何东西,只是单独运行debug里的exe文件而已啊
Helloworld3020 2020-12-25
  • 打赏
  • 举报
回复
引用
app.config配置中加入 <configuration> <system.windows.forms jitDebugging="true" /> </configuration> 试试。
试过了,还是不行
Cpx_CsdnLt 2020-12-25
  • 打赏
  • 举报
回复
你引用的是不是有第三方的dll没有复制到本地啊
dugupiaoyun 2020-12-24
  • 打赏
  • 举报
回复
应该是你的系统的问题,将修改生成的平台改为x86,而不是any cpu。然后重新编译,再试试单独运行Debug里的exe吧!
Let's GO GO GO 2020-12-24
  • 打赏
  • 举报
回复
显然引用 的东西被你删除了。DEBUG全部拿走就可以了
耗子哭死猫 2020-12-23
  • 打赏
  • 举报
回复
app.config配置中加入 <configuration> <system.windows.forms jitDebugging="true" /> </configuration> 试试。
Helloworld3020 2020-12-23
  • 打赏
  • 举报
回复
谢谢回答。 我重复试了几遍你说的方法,没有报错,但是单独单独运行Debug文件夹中的exe还是有问题。
加载更多回复(1)

111,088

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧