2012-04-14 77 views
6

我學習C#和控制檯應用程序運行Hello World程序運行Hello World程序。當我使用f5運行時,控制檯彈出並立即消失。無法在C#控制檯應用程序

using System; 

namespace CSharpConsoleApps 
{ 
    class Program 
    { 
     static void Main() 
     { 
      Console.WriteLine("hello c sharp world "); 
     } 
    } 
} 

下面是它的顯示輸出

'CSharpConsoleApps.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'CSharpConsoleApps.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities\10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'CSharpConsoleApps.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Windows.Forms\v4.0_4.0.0.0__b77a5c561934e089\System.Windows.Forms.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'CSharpConsoleApps.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'CSharpConsoleApps.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'CSharpConsoleApps.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities.Sync\10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.Sync.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'CSharpConsoleApps.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.Debugger.Runtime\10.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.Debugger.Runtime.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'CSharpConsoleApps.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Users\skumar\Documents\Visual Studio 2010\Projects\CSharpConsoleApps\CSharpConsoleApps\bin\Debug\CSharpConsoleApps.vshost.exe', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'CSharpConsoleApps.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'CSharpConsoleApps.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml.Linq\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.Linq.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'CSharpConsoleApps.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Data.DataSetExtensions\v4.0_4.0.0.0__b77a5c561934e089\System.Data.DataSetExtensions.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'CSharpConsoleApps.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\Microsoft.CSharp\v4.0_4.0.0.0__b03f5f7f11d50a3a\Microsoft.CSharp.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'CSharpConsoleApps.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_32\System.Data\v4.0_4.0.0.0__b77a5c561934e089\System.Data.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
'CSharpConsoleApps.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. 
The thread '<No Name>' (0x12b0) has exited with code 0 (0x0). 
The thread 'vshost.LoadReference' (0x674) has exited with code 0 (0x0). 
'CSharpConsoleApps.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Users\skumar\Documents\Visual Studio 2010\Projects\CSharpConsoleApps\CSharpConsoleApps\bin\Debug\CSharpConsoleApps.exe', Symbols loaded. 
The thread 'vshost.RunParkingWindow' (0xdd8) has exited with code 0 (0x0). 
The thread '<No Name>' (0x1424) has exited with code 0 (0x0). 
The program '[4320] CSharpConsoleApps.vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0). 

回答

17

你的計劃成功執行的消息,但控制檯關閉rightaway。

爲了防止這種情況,使用Ctrl+F5,而不是隻是F5

+0

Wow..it的working..thanks對您有所幫助 – JavaGeek 2012-04-14 10:01:50

+0

最簡單而有用的答案。謝謝 – dens14345 2016-06-01 13:35:32

6

即工作完全按照你告訴它!

基本上,代碼只是說:「這顯示一行文字」。所以一旦完成,沒有別的事情可做,它會停止運行。

嘗試增加

Console.ReadKey(); 

並重新運行

+0

是的。它正在工作。感謝您的幫助 – JavaGeek 2012-04-14 10:02:14

1

電腦也正是他們被編程的事情。既然如此,原始代碼並未指示計算機在終止之前等待來自用戶的輸入。所以,計算機按照提示進行操作,並正常結束程序。

你可以嘗試:

namespace CSharpConsoleApps 
{ 
    class Program 
    { 
     static void Main() 
     { 
      Console.WriteLine("hello c sharp world "); 
      Console.ReadLine(); 
     } 
    } 
} 

並重新運行。計算機將等待您在關閉控制檯窗口之前按ENTER鍵。

+0

這不提供問題的答案。要批評或要求作者澄清,在他們的帖子下留下評論 - 你總是可以評論你自己的帖子,一旦你有足夠的[聲譽](http://stackoverflow.com/help/whats-reputation),你會能夠[評論任何帖子](http://stackoverflow.com/help/privileges/comment)。 – Rich 2014-04-29 06:37:29

+0

好的,我需要多少細節才能回答這個問題?上面的評論回答了爲什麼控制檯窗口消失的問題,所以再次解釋它是多餘的。 – spfastjr 2014-04-30 09:36:42

+0

奇怪的是,我不能看到審查我的歷史,所以我不,如果它是爭議,拒絕或標記爲有幫助的。如果我再次看到這個,我可能不會將其標記爲「不是答案」。 – Rich 2014-04-30 09:52:20