473,749 Members | 2,432 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# - Installer - Sample code to run batch file via custom action

Hi all,

Here's some work in progress that should allow you to run a batch file as a
custom action in a VS deployment project. Yup I know you can use js or wsh,
but the target may not have either.. Essentially it's just a wrapper for the
Process class and a command interpreter.

Warning, it only partly works. I had wanted to pass in

(a) The name of the batch file (through "BatchFileName" ), and
(b) A set of arguements for the batch file (through "BatchFileArgs" )

via the CustomActionDat a property exposed in the VS designer

eg.

/BatchFileName="[TARGETDIR]\InstallThirdPa rty.bat"

Unfortunately, I can't find a way to pass embedded double-quote characters
through BatchFileArgs. Passing embedded quotes is necessary when the
arguements represent paths with embedded spaces.. I'd love to know if anyone
has a workaround.

using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Configur ation.Install;
using System.Diagnost ics;
using System.IO;

namespace InstallHelper
{
[RunInstaller(tr ue)]
public class BatchBootStrap : System.Configur ation.Install.I nstaller
{
Process mCmdInterpreter = new Process();

public override void Install(IDictio nary savedState)
{
Trace.WriteLine ("InstallHelper .BatchBootStrap .Install - enter");
string BatchFileName = Context.Paramet ers["BatchFileN ame"];

try
{
base.Install(sa vedState);

Path.IsPathRoot ed(BatchFileNam e); // throws ArgumentExcepti on if
BatchFileName contains any InvalidPathChar acters

if (!File.Exists(B atchFileName))
throw new Exception(strin g.Format("Batch file \"{0}\", does not
exist", BatchFileName)) ;

mCmdInterpreter .StartInfo.File Name =
Environment.Get EnvironmentVari able("ComSpec") ;

if (!File.Exists(m CmdInterpreter. StartInfo.FileN ame))
throw new Exception(strin g.Format("Comma nd interpreter \"{0}\" not
present, probably non standard or corrupted OS installation",
mCmdInterpreter .StartInfo.File Name));

string BatchFileArgs = Context.Paramet ers["BatchFileA rgs"];

// The ComSpec arguement format string supports space delimeted
double-quote framed args
// BatchFileArgs represents a space delimited list of arguements.
// Individual path / file name arguements that are passed via
BatchFileArgs must be "" framed if they contain spaces
// eg. BatchFileArgs could be the following '"C:\Test Dir\"
C:\TestDir2 123'
//

mCmdInterpreter .StartInfo.Argu ments = string.Format("/c \"\"{0}\"
{1}\"", BatchFileName, BatchFileArgs);
mCmdInterpreter .StartInfo.UseS hellExecute = false;
mCmdInterpreter .StartInfo.Redi rectStandardOut put = true;
//mCmdInterpreter .StartInfo.Redi rectStandardErr or = true;

mCmdInterpreter .Start();

Trace.WriteLine (string.Format( "InstallHelper. BatchBootStrap. Install
- Running batch file \"{0}\"\n Args ==> {1}", BatchFileName, BatchFileArgs)) ;
mCmdInterpreter .WaitForExit(); // blocking call

string StandardOutput = mCmdInterpreter .StandardOutput .ReadToEnd();
Trace.WriteLine (string.Format( "InstallHelper. BatchBootStrap. Install
- Batch file complete, output was as follows..\n{0}" , StandardOutput) );

string StandardOutputL ogFileName =
string.Format(" {0}.{1:yyyy_MM_ dd_a\\t_HH_mm_s s}.log", BatchFileName,
DateTime.Now);
Trace.WriteLine (string.Format( "InstallHelper. BatchBootStrap. Install
- Trying to log output to \"{0}\"", StandardOutputL ogFileName));

try
{
StreamWriter StandardOutputL og = new
StreamWriter(St andardOutputLog FileName);
StandardOutputL og.Write(Standa rdOutput);
StandardOutputL og.Flush();
StandardOutputL og.Close();
Trace.WriteLine (string.Format( "{0}",
"InstallHelper. BatchBootStrap. Install - log written successfully")) ;
}
catch (Exception LogErr)
{
Trace.WriteLine (string.Format(
"InstallHelper. BatchBootStrap. Install - An exception occurred
whilst trying to log output to \"{0}\", {1}\n Check user priviledge and
quotas.",
StandardOutputL ogFileName,
LogErr.Message) );
}
}
catch (Exception Err)
{
string DetailedErrMess age = string.Format(
"InstallHelper. BatchBootStrap. Install - A fatal exception occurred
whilst trying to run batch file \"{0}\". {1}",
BatchFileName,
Err.Message);
Trace.WriteLine (DetailedErrMes sage);
throw(new Exception(Detai ledErrMessage, Err));
}
finally
{
Trace.WriteLine ("InstallHelper .BatchBootStrap .Install - exit");
}
}

public BatchBootStrap( ) : base()
{
}
}
}

May 29 '06 #1
1 24307
"steve" <st***@discussi ons.microsoft.c om> wrote in message
news:88******** *************** ***********@mic rosoft.com...
Hi all,

Here's some work in progress that should allow you to run a batch file as
a
custom action in a VS deployment project. Yup I know you can use js or
wsh,
but the target may not have either.. Essentially it's just a wrapper for
the
Process class and a command interpreter.

Warning, it only partly works. I had wanted to pass in

(a) The name of the batch file (through "BatchFileName" ), and
(b) A set of arguements for the batch file (through "BatchFileArgs" )

via the CustomActionDat a property exposed in the VS designer

eg.

/BatchFileName="[TARGETDIR]\InstallThirdPa rty.bat"

Unfortunately, I can't find a way to pass embedded double-quote characters
through BatchFileArgs. Passing embedded quotes is necessary when the
arguements represent paths with embedded spaces.. I'd love to know if
anyone
has a workaround.
<skip>
string BatchFileArgs = Context.Paramet ers["BatchFileA rgs"];

// The ComSpec arguement format string supports space delimeted
double-quote framed args
// BatchFileArgs represents a space delimited list of arguements.
// Individual path / file name arguements that are passed via
BatchFileArgs must be "" framed if they contain spaces
// eg. BatchFileArgs could be the following '"C:\Test Dir\"
C:\TestDir2 123'
//

mCmdInterpreter .StartInfo.Argu ments = string.Format("/c \"\"{0}\"
{1}\"", BatchFileName, BatchFileArgs);


why doing so? if parameter ( BatchFileArgs ) has value "C:\test 123" adding
more double-quotes?

You can detect if there are double quotes in the BatchFileArgs, an
appropriately preprocess it ( add or remove double quotes )
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
May 29 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
4928
by: Arun Bhalla | last post by:
I'm working with VS.NET 2003 and .NET 1.1 (not SP1) on Windows XP SP1. My application is using the Windows Installer Bootstrap. (I may have also installed a module which detects requirements (.NET 1.1 -> MsiNetAssembly Support.) I have a few issues and questions regarding the MSI and its properties. * This may be my biggest annoyance at the moment. When the installer creates the target directory, it's setting them as read-only. When the...
0
2104
by: Arun Bhalla | last post by:
I'm having some inconsistency problems with my deployment project ("Setup") and its custom actions ("Installer"). I'm using Visual Studio .NET 2003 (.NET 1.1, no service pack) on Windows XPSP1. My main project is a band object (Explorer Bar) in an assembly (.DLL) that needs to be registered on installation and unregisted on uninstallation. In addition, during installation, Setup creates registry subkeys, sets registry values, installs...
2
5365
by: MENTAT | last post by:
Hi, I am trying to create an installer for my web application. So I added a web setup project to my solution (I am using VS.NET 2003). Been playing around with it since then and it basically works. Now, I am wondering if there is any way to change the directory that the installer installs the files to. I want to be able to put my files in a specified directory (user specified is good, but i don't mind hardcoding the path either) and...
1
8477
by: Craig | last post by:
I have added a 'Textboxes (A)' to my UI installer project along with a custom action to pass the value back to a class I've written to override the void Install function. As long as the text is very simple everything is working just fine. I've tested it by writing the string out to a text file to test it. However, the trouble comes in when I use the textbox for what I really intended, a ADO.Net Connection String. The textbox is to...
1
3796
by: BuddyWork | last post by:
Hello, When a particular user (has administrator rights) on a Windows 2000 Server SP4 tries to run any MSI's we get the message mentioned in the subject. If we logon with another user that has administrator rights then it works, if we create a new administrator user then it does NOT work, Here is the MSI log, please help as this is causing problems as only 3 users on this box are able to run MSI where are about 15 other users
3
2336
by: DC Gringo | last post by:
Hi, I'm trying to use a custom action to modify a database (rather than create one) using the VS.NET '03's help example called "Custom Action to Create Database During Installation". I've made two modifications to the sample in the document...both are in the "Protected Sub AddDBTable" (towards the bottom). I've changed ' Creates the database.
3
1714
by: Mark Assousa | last post by:
Hi, I'm having trouble finding the right news group for this posting so here goes... I am attempting to execute some special actions within an installation project for a standard .Net Windows application. I have a custom Installer class that needs to access a disk file installed during the installation in a directory relative to the application directory. The file name is known but I don't know how to access the target
4
6432
by: Shiraz | last post by:
Hi I'm using Visual Studio Installer to make my installer, and have not as yet figured out a straightforward way to use it to set environmental variables. Amongst the various things I tried, I'm thinking the following might help. I would appreciate if someone could comment on this idea and possibly suggest a better one: The environement variable in question is 'Path' in the HKCU registry folder's Environment key. I want to add some...
0
1568
by: kplkumar | last post by:
Hi This is what we have, our MSI has custom actions which are .vbs which essentially calls a batch file that in turn calls .sql files to install the database. The vbs file has something like, Set WshShell = CreateObject("WScript.Shell") WshShell.Run "\Somepath\DB_Setup.bat",1,TRUE
0
8997
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8833
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9568
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9389
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8257
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6079
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4709
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3320
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.