2016-09-23 84 views
0

正在開發一個我的項目。我發現需要有正常的控制檯應用來更改數字並將其發佈出去。我發現一篇文章讓這很容易,但是它不會輸出任何內容。它打開控制檯應用程序和名稱更改,但沒有輸出。Xna沒有寫入控制檯

這是我用它來打開它

[DllImport("kernel32")] 
    static extern bool AllocConsole(); 

    private void UseConsole(object sender) 
    { 
     AllocConsole(); 
     Console.Title = "Output"; 
     Console.Write("hello world"); 
    } 

如果你知道什麼可能幫得到的輸出。這將是巨大的 由於已經

回答

-1

試圖創建一個控制檯與我的XNA/FNA應用程序時我有同樣的問題。找到的所有常見解決方案都無效:書寫的控制檯行被打印到Visual Studio(社區)的「輸出」窗口中,但是當通過直接執行遊戲(在Visual Studio之外)啓動應用程序時,控制檯窗口仍然空着。通過https://stackoverflow.com/users/1087922/zunair

https://stackoverflow.com/a/34170767複製的代碼:對類似的問題下面的答案爲我工作

using System; 
using System.Windows.Forms; 
using System.Text; 
using System.IO; 
using System.Runtime.InteropServices; 
using Microsoft.Win32.SafeHandles; 

namespace WindowsApplication 
{ 
    static class Program 
    { 
     [DllImport("kernel32.dll", 
      EntryPoint = "GetStdHandle", 
      SetLastError = true, 
      CharSet = CharSet.Auto, 
      CallingConvention = CallingConvention.StdCall)] 
     private static extern IntPtr GetStdHandle(int nStdHandle); 
     [DllImport("kernel32.dll", 
      EntryPoint = "AllocConsole", 
      SetLastError = true, 
      CharSet = CharSet.Auto, 
      CallingConvention = CallingConvention.StdCall)] 
     private static extern int AllocConsole(); 
     private const int STD_OUTPUT_HANDLE = -11; 
     private const int MY_CODE_PAGE = 437; 

     static void Main(string[] args) 
     { 
      Console.WriteLine("This text you can see in debug output window."); 

      AllocConsole(); 
      IntPtr stdHandle=GetStdHandle(STD_OUTPUT_HANDLE); 
      SafeFileHandle safeFileHandle = new SafeFileHandle(stdHandle, true); 
      FileStream fileStream = new FileStream(safeFileHandle, FileAccess.Write); 
      Encoding encoding = System.Text.Encoding.GetEncoding(MY_CODE_PAGE); 
      StreamWriter standardOutput = new StreamWriter(fileStream, encoding); 
      standardOutput.AutoFlush = true; 
      Console.SetOut(standardOutput); 

      Console.WriteLine("This text you can see in console window."); 

      MessageBox.Show("Now I'm happy!"); 
     } 
    } 
}