none
Convert Script Bat to c# RRS feed

  • Question

  • hi

    i need help how to converting script bat to c#

    @shift /0
    @echo off
    
    reg query HKEY_LOCAL_MACHINE\Software\imotech
    if %ERRORLEVEL% EQU 0 goto DELREG
    
    reg query HKEY_CURRENT_USER\Software\imotech
    if %ERRORLEVEL% EQU 0 goto DELREG2
    
    goto BIT
    
    :DELREG
    reg DELETE HKEY_LOCAL_MACHINE\Software\imotech /f
    rem Wait 20
    
    reg query HKEY_CURRENT_USER\Software\imotech
    if %ERRORLEVEL% EQU 0 goto DELREG2
    goto BIT
    
    :DELREG2
    reg DELETE HKEY_CURRENT_USER\Software\imotech /f
    rem Wait 20
    goto BIT
    
    :BIT
    if exist "%PROGRAMFILES(X86)%" goto RUN64
    goto RUN32
    
    :RUN32
    "%CD%\x86.exe" 24\10\2500 "%CD%\XXX.exe"
    goto ABOUT
    
    :RUN64
    "%CD%\x64.exe" 24\10\2500 "%CD%\XXX.exe"
    goto ABOUT
    
    :ABOUT
    cls
    echo -------------------------------------
    echo - Software Name : XXX 1.5.x -
    echo -           Version : v1.2          -
    echo -------------------------------------
    timeout 3
    

    Sunday, August 12, 2018 6:03 AM

All replies

  • May I ask why? And did you consider using PowerShell?

    Otherwise, you use can RegistryKey.DeleteSubKey to remove an entire registry subkey. And you can use the Process class to start programs.

    Something like

    namespace ConsoleCS
    {
        using System;
        using System.Diagnostics;
        using Microsoft.Win32;
    
        public class Program
        {
            public static void Main(string[] args)
            {
                Registry.LocalMachine.DeleteSubKeyTree(@"Software\imotech");
                Registry.CurrentUser.DeleteSubKeyTree(@"Software\imotech");
                if (Environment.Is64BitOperatingSystem)
                {
                    StartExecutable(@"%CD%\x64.exe", @"24\10\2500 ""%CD%\XXX.exe""");
                }
                else
                {
                    StartExecutable(@"%CD%\x86.exe", @"24\10\2500 ""%CD%\XXX.exe""");
                }
    
                Console.WriteLine("Done.");
                Console.ReadLine();
            }
    
            private static void StartExecutable(string fileName, string arguments)
            {
                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.FileName = fileName;
                startInfo.Arguments = arguments;
                using (Process process = new Process())
                {
                    Process.Start(startInfo);
                }
            }
        }
    }

    Obviously, you need to add error handling.

    p.s. when you control the installer and %CD% points to a path on hard disk or SSD, then you should use the correct paths. X86 programs are installed in %PROGRAMFILES(X86)% (normally C:\Program Files (x86)) and 64-bit programs are installed in %PROGRAMFILES% (normally C:\Program Files). 

    • Proposed as answer by Cherkaoui.Mouad Thursday, September 6, 2018 3:50 PM
    Sunday, August 12, 2018 12:31 PM