Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm Trying to hide cleanmanager when started via CMD but I can't hide it... It is still showing up and won't go away until it's finished.
How can I fix this? Been looking up for answers for hours but can't find a solution.

C#
public partial class MainWindow : Window
{
    [DllImport("User32")]
    private static extern int ShowWindow(int hwnd, int nCmdShow);
    private const int SW_HIDE = 0;

    public MainWindow()
    {

        InitializeComponent();
        test();
        AdvancedClean();

    private void AdvancedClean()
    {
        string filename = "advancedcleanmgr.reg";
        if (!System.IO.File.Exists(filename))
            System.IO.File.WriteAllText(filename, Properties.Resources.advancedcleanmgr);
        Process regeditProcess = Process.Start("regedit.exe", "/s advancedcleanmgr.reg");
        regeditProcess.WaitForExit();
        while (!regeditProcess.HasExited)
            Thread.Sleep(500);
        System.Diagnostics.Process process = new System.Diagnostics.Process();
        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
        startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        startInfo.FileName = "cmd.exe";
        startInfo.Arguments = "/C attrib advancedcleanmgr.reg +s +h & cleanmgr sagerun:6553";
        process.StartInfo = startInfo;
        process.Start();
        Test();
        process.WaitForExit();

    }

    private void test()
    {
        DispatcherTimer dispatcherTimer = new DispatcherTimer();
        dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
        dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
        dispatcherTimer.Start();
    }

    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
        int hWnd;
        Process[] processRunning = Process.GetProcesses();
        foreach (Process pr in processRunning)
        {
            if (pr.ProcessName == "cleanmgr")
            {
                hWnd = pr.MainWindowHandle.ToInt32();
                ShowWindow(hWnd, SW_HIDE);
            }
        }
    }
Posted
Comments
Sergey Alexandrovich Kryukov 8-Apr-13 10:10am    
Why doing all that? And of course you cannot hide an application, at least like that. Why?!
—SA
klottim 8-Apr-13 10:16am    
Because the cleanmgr is supposed to work in background.
Im creating a cleaning utility with other features and this is the only problem. That cleanmgr is showing up.

If I start cleanmgr by myself and then start the utility, the program successfully hides cleanmgr. However if it is started via cmd like on my code it can't be hidden so is there any solution for that?

1 solution

C#
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C attrib advancedcleanmgr.reg +s +h & cleanmgr sagerun:6553";


The effect of this section of code is to start a hidden instance of cmd.exe and execute
1) attrib.exe followed by 2) cleanmgr.exe

The output of attrib.exe won't be seen as the output console is hidden, but cleanmgr being a GUI program, will display it's own interface.

The solution should be to start cleanmgr directly without the help of cmd.exe
C#
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cleanmgr.exe";
startInfo.Arguments = "sagerun:6553";


That said, when I tried it on my XP system, cleanmgr's initial window was hidden but the individual progress windows displayed as each disk drive was processed were still visible.

Alan.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900