简体   繁体   English

C# 程序不会作为 .EXE 在 visual studio 之外启动

[英]C# program wont start outside visual studio as a .EXE

I have created a small program in C# winforms that runs fine when i start it in visual studio 2017. But when I build the solution and doubleclick the.exe, nothing happens, no screen appears, even task manager doesn't see it.我在 C# winforms 中创建了一个小程序,当我在 visual studio 2017 中启动它时运行良好。但是当我构建解决方案并双击 .exe 时,没有任何反应,没有屏幕出现,甚至任务管理器也看不到它。 No errors.没有错误。 It's like it doesn't do anything, My only guess is that I build it wrong because I used Nuget to install newtonsofts JSON.NET in te solution?好像它什么也没做,我唯一的猜测是我构建错误,因为我在 te 解决方案中使用 Nuget 安装 newtonsofts JSON.NET? do I need to do anything different or should just building the solution work?我需要做任何不同的事情还是应该只构建解决方案?

[solved] [解决了]

today i learned the difference between the bin and obj folder, thanks to everyone for helping今天知道了bin和obj文件夹的区别,感谢大家的帮助

Based on your comment: 根据您的评论:

it is in the obj/debug folder of the project 它在项目的obj / debug文件夹中

It sounds like you're running the wrong .exe. 听起来您运行的是错误的.exe。 The obj folder is used for temporary/misc. obj文件夹用于临时/杂项。 files from the build process (see What is obj folder generated for? ). 构建过程中的文件(请参阅生成obj文件夹的目的是什么? )。

Instead, you want to run the exe within bin\\Debug , if "Debug" is the configuration you're building for. 相反,如果要为“ Build”构建配置,则要在bin\\Debug运行exe。 You can see which configuration at the top of VS. 您可以在VS顶部看到哪种配置。
在此处输入图片说明

Like others have also mentioned, make sure that Newtonsoft.Json.dll is being copied to that output directory as well. 就像其他人也提到的那样,请确保将Newtonsoft.Json.dll也复制到该输出目录。 Programs and their dependencies need to be together, generally speaking. 一般来说,程序及其依赖项必须在一起。 Otherwise, your exe will not know where to find the JSON code it needs to function. 否则,您的exe将不知道在哪里可以找到需要运行的JSON代码。

99% of the time, you should pretend the obj directory isn't even there. 99%的时间,您应该假装obj目录不存在。

If that still isn't pointing you in the right direction, run the app from a command window. 如果仍然无法为您指明正确的方向,请从命令窗口运行该应用程序。 Any exception should get printed to it and the window will remain open for you to examine (and this has the benefit of not needing any additional logging or exception handling code to see this error). 任何异常都应打印到该窗口上,并且该窗口将保持打开状态供您检查(这具有不需要任何其他日志记录或异常处理代码即可看到此错误的好处)。
For example, I wrote up a bad application that get a NullReferenceException in a method called Test that is called from Main . 例如,我编写了一个错误的应用程序,该应用程序在从Main调用的称为Test的方法中获得了NullReferenceException As you can see, the stacktrace is easily visible, even though my app has crashed (credit to ColinM for bringing this up originally). 如您所见,即使我的应用程序崩溃了,堆栈跟踪也很容易看到(向ColinM致谢是因为它最初用于启动)。
在此处输入图片说明

I believe that there's a problem with the startup module. 我相信启动模块有问题。 Follow the steps below 请按照以下步骤

  1. Open your Solution in visual studio 在Visual Studio中打开您的解决方案
  2. Double click on properties 双击属性

    在此处输入图片说明

  3. Select output type to Windows Application 选择Windows应用程序的输出类型

  4. Make sure to set the startup object as follows 确保如下设置启动对象

    在此处输入图片说明

I hope it helps 希望对您有所帮助

I think there is only one reason 我认为只有一个原因

There is a command line argument predefined in Visual Studio. 在Visual Studio中预定义了命令行参数 Your application uses this argument to be executed, without it, it closes itself too quickly and you even can't see your application opened. 您的应用程序使用该参数来执行,如果没有该参数,它会很快关闭自身,甚至看不到您的应用程序已打开。

Right click on your project in VS -> Properties -> Debug and see if there is a value in command line arguments Right click on your project in VS -> Properties -> Debug然后查看命令行参数中是否有值

exe and their supporting files should be in the bin folder. exe及其支持文件应位于bin文件夹中。 Do not copy only exe from bin folder and try to run it. 不要仅从bin文件夹复制exe并尝试运行它。 It is a good idea to write some exception code to get the detail. 编写一些异常代码以获取详细信息是一个好主意。

For future reference, yet another reason (that I have experienced) can be为了将来参考,另一个原因(我经历过)可以是

System.Diagnostics.Debug.Assert();

statements.声明。 In my case, the program executed normally when started from VS but when I run it by clicking its.exe (created in the Debug Mode) then it hung/freezed.在我的例子中,程序从 VS 启动时正常执行,但当我通过单击 its.exe(在调试模式下创建)运行它时,它挂起/冻结。 No exceptions, no printed logs.没有例外,没有打印日志。 Frustrating.令人沮丧。 Then I checked the Windows Event Viewer (Our true friend).然后我检查了 Windows 事件查看器(我们真正的朋友)。 It explicitly displayed the problem and the culprit was a Debug.Assert() statement.它明确显示了问题,罪魁祸首是 Debug.Assert() 语句。

The lesson learned again: Check再次吸取教训:检查

Windows Event Viewer > Windows Logs > Application Windows 事件查看器 > Windows 日志 > 应用程序

especially when your app hangs/freezes/deadlocks or when no app logs are available.特别是当您的应用程序挂起/冻结/死锁或没有可用的应用程序日志时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM