Archiwizacja folderu z paskiem postępu oraz wyświetlenie archiwum na pierwszym planie

0

Cześć wszystkim,
Potrzebuję program który spakuje folder do archiwum wyświetlając przy tym pasek postępu, a następnie otworzy to archiwum na pierwszym planie. Program musi poprawnie pracować na windowsie 10 w trybie tabletu i tutaj jest problem z którym nie mogę sobie poradzić.
Napisałem program jak niżej, niestety czasami otwiera się archiwum na pierwszym planie a czasem zostaje na pasku - masakra z tym trybem tabletu.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Compression;
using System.Threading;
using System.Threading.Tasks;
using System.ComponentModel;

namespace PoprawioneKartyCheck
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		public MainForm()
		{
			InitializeComponent();
		}
		[System.Runtime.InteropServices.DllImport("User32.dll")]
		private static extern bool SetForegroundWindow(IntPtr handle);
		[System.Runtime.InteropServices.DllImport("User32.dll")]
		private static extern bool ShowWindow(IntPtr handle, int nCmdShow);
		[System.Runtime.InteropServices.DllImport("User32.dll")]
		private static extern bool IsIconic(IntPtr handle);
		
		public async void MainFormLoad(object sender, EventArgs e)
		{
			string filePath = @"C:\Program Files\7-Zip\7z.exe";
			string sciezkapakowania = @"2";
			string spakowanyfolder = @"1.7z";
			
			try 
			{				
				ProcessStartInfo startInfo = new ProcessStartInfo();
				{
					startInfo.UseShellExecute = false;
					startInfo.RedirectStandardOutput = true;
					startInfo.CreateNoWindow = true;
					startInfo.FileName = filePath;
					startInfo.Arguments = "u -uq0 -t7z \"" + spakowanyfolder + "\" \"" + sciezkapakowania + "\" -mx=1";
					progressBar1.Style = ProgressBarStyle.Marquee;
					progressBar1.MarqueeAnimationSpeed = 20;
					label3.Text = "Proszę czekać, zaraz otworzę";
				}
				
				using (Process process = new Process {StartInfo = startInfo})
					await Task.Run (() =>
					                {
					                	process.Start();
					                	process.WaitForExit();
					                });
				progressBar1.Style = ProgressBarStyle.Continuous;
				progressBar1.MarqueeAnimationSpeed = 0;
				
				Process proc = Process.Start(@"1.7z");
				
				const int SW_RESTORE = 9;
				
				IntPtr handle = proc.MainWindowHandle;
				if (IsIconic(handle))
				{
					ShowWindow(handle, SW_RESTORE);
				}
				SetForegroundWindow(handle);
				
				Application.Exit();
					
			} 
			catch (Exception ex) 
			{
				MessageBox.Show("Wystąpił błąd:" + ex.Message, "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}
		
	}
}
0

Spróbuj tego:

[DllImport("User32.dll", SetLastError = true)]
static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);

(...)

SwitchToThisWindow(handle, true);
0

Nie lepiej otworzyć ten folder za pomocą polecenia explorer $dir ?

0
jarzi napisał(a):

Spróbuj tego:

[DllImport("User32.dll", SetLastError = true)]
static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);

(...)

SwitchToThisWindow(handle, true);

Dodałem, nie działa w trybie dotykowym. Bez trybu dotykowego działa.

0xmarcin - nie wiem jeszcze, musze ogarnąć jak to zrobić w ten sposób.

0

Na sufrace go sprawdzałem i jest ok, sprawdź u siebie

public Form1()
{
    InitializeComponent();

    var p = new Process
    {
        StartInfo = new ProcessStartInfo
        {
            FileName = @"notepad++.exe",
            RedirectStandardOutput = true,
            RedirectStandardInput = true,
            UseShellExecute = false,
            CreateNoWindow = true
        }
    };

    p.Start();
    SetTopMost(p.Handle);

    p.WaitForExit();
}


static void SetTopMost(IntPtr windowHandle)
{
    if (windowHandle != IntPtr.Zero)
    {
        SetWindowPos(windowHandle, IntPtr.Zero, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW | SWP_TOPMOST);
    }
}

const uint SWP_NOSIZE = 0x0001;
const uint SWP_NOMOVE = 0x0002;
const uint SWP_SHOWWINDOW = 0x0040;
const uint SWP_TOPMOST = 0x0008;

[System.Runtime.InteropServices.DllImport("user32.dll")]
[return: System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.Bool)]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
0
jarzi napisał(a):

Na sufrace go sprawdzałem i jest ok, sprawdź u siebie

Dzięki jarzi ciężki kod jak dla mnie, ale trochę go przenalizowałem i sprawdziłem. Jak na razie problem, który widzę jest taki, że nie mogę otworzyć konkretnego archiwum tylko muszę się opierać na plikach .exe inaczej sypie błędami typu: " wybrany typ wykonywalny nie jest aplikacją tego systemu operacyjnego".
Co ciekawego jeśli w poprzednim kodzie zacząłem wywoływać okno z przycisku, a nie z FormLoad to okno się maksymalizowało - tylko już sam nie wiem czy to przypadek czy fakt :).

1 użytkowników online, w tym zalogowanych: 0, gości: 1