cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
DanBerg
Level 3

Automation using C# to change PathVariable

I have several projects where i need to update the PathVariable before creating my MSI. I had been doing this using vbscript but I now want to switch over to using a Custom Task in TFS Team Build.

I can't seem to get my program to work however. One problem is When i am creating my task ISWiPathVariables does not have an 'Item' member like the documentation states. I tried to work around it by looping through all objects in the ISWiPathVariables collection but I cannot create the ISWiPathVariable object. I get an error stating there is no default constructor.

Here is a trimmed down version of my code to make it easier to read. Can anyone tell me what the problem could be? I know i must just be missing something obvious.

I am using InstallShield 2009 and I have already added a reference to ISWiAutomation15.dll in my solution. I already have a task that updates the Version in the same Solution so at least something works.

[CODE]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ISWiAuto15;
using System.IO;

namespace SetPathVariable
{
class Program
{
static void Main(string[] args)
{
bool success = false;
int ISOpenSuccess = int.MinValue;
int ISSaveSuccess = int.MinValue;
string ISMPath = @"Z:\PathToInstaller.ism";
string PathValue = @"D:\SomePath\";
string PathVariable = "PATH_TO_FILES";

string IsmName = Path.GetFileName(ISMPath);

ISWiAuto15.ISWiProject oProject = new ISWiAuto15.ISWiProject();

try
{
// Open the ISM so we can update the PathVariable
oProject.OpenProject(ISMPath, false);

ISWiAuto15.ISWiPathVariables pathVariables = new ISWiPathVariables();

pathVariables = oProject.ISWiPathVariables;

//Throws a no default constructor error
ISWiAuto15.ISWiPathVariable path = new ISWiPathVariable();

for (int i = 0; i < pathVariables.Count; i++)
{
path.Name = pathVariables.Name;
path.Value = pathVariables.Value;
if (path.Name.Equals(PathVariable))
{
path.Value = PathValue;
break;
}
}
oProject.SaveProject();
}
catch (Exception ex)
{
throw;
}
finally
{
oProject.CloseProject();
}
}
}
}
[/CODE]
Labels (1)
0 Kudos
(1) Reply
DanBerg
Level 3

I figured this out. It turns out the collection is 1 based not 0 based.

Just in case anyone is trying to do something similar here is the updated code that works:

[CODE]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ISWiAuto15;
using System.IO;

namespace SetPathVariable
{
class Program
{
static void Main(string[] args)
{
bool success = false;
int ISOpenSuccess = int.MinValue;
int ISSaveSuccess = int.MinValue;
string ISMPath = @"Z:\PathToInstaller.ism";
string PathValue = @"D:\SomePath\";
string PathVariable = "PATH_TO_FILES";

string IsmName = Path.GetFileName(ISMPath);

ISWiAuto15.ISWiProject oProject = new ISWiAuto15.ISWiProject();

try
{
// Open the ISM so we can update the PathVariable
oProject.OpenProject(ISMPath, false);

ISWiAuto15.ISWiPathVariables pathVariables = new ISWiPathVariables();

pathVariables = oProject.ISWiPathVariables;

for (int i = 1; i <= pathVariables.Count; i++) //collection is 1 based
{
if (pathVariables.Name.Equals(PathVariable))
{
pathVariables.Value = PathValue;
break;
}
}
oProject.SaveProject();
}
catch (Exception ex)
{
throw;
}
finally
{
oProject.CloseProject();
}
}
}
}
[/CODE]

Another thing is you need to make sure you compile to x86 if you want this to run on a 64bit OS.
0 Kudos