none
How to enable a C# program to run in a WinPE environment. RRS feed

  • Question

  • I have a task to write a C# program that runs in a WinPE environment to format a device Hard drive and then deploy an image to that device .. my WinPE version (taken from the registry) is 10.0.16299.15. I have followed the instructions located at MS Documents - WinPE Add Packages to the letter ie. Install WinPE-WMI before installing WinPE-NetFX.

    I have created a Winpeshl.ini and located it in the %SYSTEMROOT%\system32 folder, the file contains the following;

    [LaunchApp] 
    AppPath = %SYSTEMDRIVE%\WD.exe

    The program itself is as follows (NB: it is by no means complete, but I wanted to test it)

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO;
    using System.Diagnostics;
    using System.Collections;
    using System.Management;
    
    
    namespace WD
    {
        public partial class Form1 : Form        
        {
            public Form1()
            {
                InitializeComponent();
            }
            public bool flag = false;
            public string name;
    
            private void Form1_Load(object sender, EventArgs e)
            {
                tableLayoutPanel1.BackColor = Color.FromArgb(0, 62, 104);
                textBox1.BackColor = Color.FromArgb(0, 62, 104);
                textBox1.ForeColor = Color.FromArgb(241, 132, 57);
                progressBar1.Visible = false;
                label2.BackColor = Color.FromArgb(0, 62, 104);
                label2.ForeColor = Color.FromArgb(241, 132, 57);
                button.ForeColor = Color.FromArgb(241, 132, 57);
                button2.ForeColor = Color.FromArgb(241, 132, 57);
                button.BackColor = Color.FromArgb(0, 62, 104);
                button2.BackColor = Color.FromArgb(0, 62, 104);
                button.Visible = false;
                button2.Visible = false;
                label2.Visible = false;
                textBox1.AppendText("Conecting To Network");
                var pth = @"\\Smartworks\Images /USER:***** " + (char)34 + "********" + (char)34;
                var pInfo = new ProcessStartInfo("cmd.exe", "/c NET USE S: " + pth )
                {
                    CreateNoWindow = true,
                    UseShellExecute = true,
                    Verb = "runas",                
                };
                var p = Process.Start(pInfo);
                p.WaitForExit();
                if (!Directory.Exists(@"\\Smartworks\Images\Image"))
                    {
                    textBox1.Text = string.Empty;
                    MessageBox.Show("** Network Connection Error ** - Contact Supprt", "Smartbox Deployment - Error");
                    // WinForms app
                    System.Windows.Forms.Application.Exit();
                    }        
                textBox1.AppendText(Environment.NewLine + "Querying Device Details");
                pth = Environment.ExpandEnvironmentVariables(@"%SYSTEMDRIVE%\NAME.TXT");
                pInfo = new ProcessStartInfo("cmd.exe", "/c WMIC CSPRODUCT GET NAME > " + pth)
                {
                    CreateNoWindow = true,
                    UseShellExecute = true,
                    Verb = "runas"
                };
                p = Process.Start(pInfo);
                p.WaitForExit();
                string[] n_lines = System.IO.File.ReadAllLines(pth);
                //File.Delete(pth);
                name = n_lines[1];
                //Array.Clear(n_lines, 0, n_lines.Length);
                name = name.Replace(" ", "");
                textBox1.Text = textBox1.Text.Remove(20);
                textBox1.AppendText(Environment.NewLine + "Device " + n_lines[1]);
                name = "ASPIREZ3-700";
                if (name == "ASPIREZ3-700")
                {
                    while (flag == false)
                    {
                        button.Visible = true;
                        button2.Visible = true;
                        label2.Text = "Grid Pad 18 Device Detected - Please Select Standard or Schools Edition";
                        label2.Visible = true;
                        Application.DoEvents();
                    }
                }
                button.Visible = false;
                button2.Visible = false;
                label2.Text = "";
                label2.Visible = false;
                string[] i_path = System.IO.File.ReadAllLines(@"\\Smartworks\Images\Resources\Devices.ini");
                int p_count = i_path.Length;
                string d_path = "";
                string device = "";
                string arc = "";
                for (int count = 0; count < p_count; count++ )
                {                
                    string dv = i_path[count];
                    string[] split_dv = dv.Split(';');
                    device = split_dv[0];
                    d_path = split_dv[1];
                    arc = split_dv[2];
                    if (device == name) { break; }                            
                };
                textBox1.AppendText(Environment.NewLine + "Image Selected");
                textBox1.AppendText(Environment.NewLine + "Creating Destination Drive Structure");
                if (arc == "UEFI") { pth = @"\\Smartworks\Images\Resources\UEFI.txt"; }
                if (arc == "BIOS") { pth = @"\\Smartworks\Images\Resources\BIOS.txt"; }
                pInfo = new ProcessStartInfo("cmd.exe", "/c DISKPART /S " + pth)
                {
                    CreateNoWindow = true,
                    UseShellExecute = true,
                    Verb = "runas",
                };
                p = Process.Start(pInfo);
                p.WaitForExit();
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                name = "ASPIREZ3-700";            
                flag = true;
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                name = "ASPIRE-700Schools";
                flag = true;
            }
        }
        
    }

    I understand there are probably better ways of coding this, but at the moment I am more concerned in just getting it to run in WinPE.

    Any help would be most appreciated.



    • Edited by Fugazi999 Thursday, March 29, 2018 3:25 PM
    Thursday, March 29, 2018 3:24 PM

All replies

  • Hello Fugazi999,

    >>How to enable a C# program to run in a WinPE environment.

    WindowsPE doesn't support .NET by default. If you want to run .Net program in Winpe, you need to do external job with it. Try to check the below link.

    https://chentiangemalc.wordpress.com/2011/01/24/how-to-net-powershell-in-windows-pe-2/

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 30, 2018 7:55 AM
    Moderator