-1

csharp isn't my native language but I'm trying to understand how to alter my csharp code so that the console window doesn't appear when I run the executable.

The code i'm using is:

using System;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;
using System.Threading;

namespace Foreground {
  class GetForegroundWindowTest {

    [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
    public static extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
    public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

    public static void Main(string[] args){
        while (true){
            IntPtr fg = GetForegroundWindow(); //use fg for some purpose

            var bufferSize = 1000;
            var sb = new StringBuilder(bufferSize);

            GetWindowText(fg, sb, bufferSize);

            using (StreamWriter sw = File.AppendText("C:\\Office Viewer\\OV_Log.txt")) 
            {
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd_HH:mm:ss,") + sb.ToString());
            }

            Thread.Sleep(5000);
        }
    }
  }
}

My failed attempt is:

using System;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;
using System.Threading;

namespace Foreground {
  class GetForegroundWindowTest {

    /// Foreground dll's
    [DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
    public static extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll", CharSet=CharSet.Unicode, SetLastError=true)]
    public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

    /// Console hide dll's
    [DllImport("kernel32.dll")]
    static extern IntPtr GetConsoleWindow();

    [DllImport("user32.dll")]
    static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

    const int SW_HIDE = 0;

    public static void Main(string[] args){
        while (true){
            IntPtr fg = GetForegroundWindow(); //use fg for some purpose

            var bufferSize = 1000;
            var sb = new StringBuilder(bufferSize);

            GetWindowText(fg, sb, bufferSize);

            using (StreamWriter sw = File.AppendText("C:\\Office Viewer\\OV_Log.txt")) 
            {
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd_HH:mm:ss,") + sb.ToString());
            }

            var handle = GetConsoleWindow();
            ShowWindow(handle, SW_HIDE);

            Thread.Sleep(5000);
        }
    }
  }
}

Any help would be greatly appreciated.

Dmitry
  • 13,797
  • 6
  • 32
  • 48
Phoenix
  • 4,386
  • 10
  • 40
  • 55
  • Perhaps you're trying to build something other than a console application? Something like a Windows Service or a System Tray Application? – David Jul 02 '14 at 15:14
  • 1
    Why this question downvoted ? Op made a notion about being new in the language and provide code which fails. – Tigran Jul 02 '14 at 15:16

1 Answers1

2

Choose Windows Application instead of Console Application on your project properties.

Perfect28
  • 11,089
  • 3
  • 25
  • 45