Question : C#: CreateNoWindow not working

I've tried CreateNoWindow = true, UseShellExecute = false and WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden but I can't get the DOS window to hide. Any suggestions?
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace HNTB_Defrag
{
    class Program
    {
        public static class CommonArea
        {
            public const string ADMIN_NAME = "HNeedles";
            public const string ADMIN_DOMAIN = "";
            public const string ADMIN_PASSWORD_CURRRENT = "Sn0wFall!";
            public const string ADMIN_PASSWORD_PREVIOUS = "R3dR0b1n!";
            public const string COMMAND_ARGUMENTS = "c: -v -f";
            public const string COMMAND_NAME = "Defrag.exe";
        }//CommanArea
 
        static void Main(string[] args)
        {
            //Information about the running process
            System.Diagnostics.ProcessStartInfo l_obj_ProcessStart 
                = new System.Diagnostics.ProcessStartInfo();
 
            
            l_obj_ProcessStart.Arguments = CommonArea.COMMAND_ARGUMENTS;
            l_obj_ProcessStart.CreateNoWindow = true;
            l_obj_ProcessStart.Domain = CommonArea.ADMIN_DOMAIN;
            l_obj_ProcessStart.ErrorDialog = false;
            l_obj_ProcessStart.UseShellExecute = false;
            l_obj_ProcessStart.FileName = System.Environment.SystemDirectory 
                + "\\" + CommonArea.COMMAND_NAME;
 
            l_obj_ProcessStart.LoadUserProfile = false;
 
            System.Security.SecureString l_objConvertString 
                = new System.Security.SecureString();
 
            foreach (char C in CommonArea.ADMIN_PASSWORD_CURRRENT)
            {
                l_objConvertString.AppendChar(C);
            }
 
            l_obj_ProcessStart.Password = l_objConvertString;
            l_obj_ProcessStart.UserName = CommonArea.ADMIN_NAME;
            l_obj_ProcessStart.UseShellExecute = false;
            l_obj_ProcessStart.WindowStyle 
                = System.Diagnostics.ProcessWindowStyle.Hidden;
            l_obj_ProcessStart.WorkingDirectory = System.Environment.SystemDirectory;
 
            System.Diagnostics.Process l_objProcess 
                = new System.Diagnostics.Process();
 
            l_objProcess.StartInfo = l_obj_ProcessStart;
 
            l_objProcess.Start();
        }//Main
   
    }
}

Answer : C#: CreateNoWindow not working

Just tested this...

Create a Windows Forms-based application, that never displays any windows, that will start defrag.exe with no window, but as the current user.

Then, from your application you can start this forms-based app with alternate credentials.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
Your Program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
 
namespace WindowsFormsApplication2
{
    static class Program
    {
        /// 
        /// The main entry point for the application.
        /// 
        [STAThread]
        static void Main()
        {
            string program = @"yourStub.exe";
            string user = "administrator";
            string password = "secret";
            string domain  = "domain";
 
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
 
            Process p = new Process();
 
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.FileName = program;
            p.StartInfo.UserName = user;
            p.StartInfo.Domain = domain;
 
            p.StartInfo.Password = new System.Security.SecureString();
            foreach (char c in password)
                p.StartInfo.Password.AppendChar(c);
 
            p.Start();
        }
    }
}
 
 
 
The stub:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
 
namespace WindowsFormsApplication1
{
    static class Program
    {
        /// 
        /// The main entry point for the application.
        /// 
        [STAThread]
        static void Main()
        {
            string program = @"defrag.exe";
            
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
 
 
            Process p = new Process();
 
 
            p.StartInfo.FileName = program;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.UseShellExecute = false;
 
            p.Start();
        }
    }
}
Random Solutions  
 
programming4us programming4us