Changing to service

  • Thread starter Thread starter Gav
  • Start date Start date
G

Gav

Hi all,

I have written a program which seriously depends on an environment variable.
When I set the env var for the system it caused me major problems unrelated
to what I was trying to achieve. So in order to get around this I just used
a simple script which would set the variable using the 'set' command and
then call my program. This worked and only held the env var for that script
session so once my program was finished it was gone and therefore not
affecting anything else...

I now want to convert this program to a windows service, all was going well
until I remembered this fine point. Is there a way I can programatically set
an environment variable just for the use of the service?

Regards
Gavin
 
Thanks for the reply however this will not get me what I'm after, I'm not
using the variable directly in my program.

I'm using an ODBC connection (third party ODBC driver) in my program when I
setup the system ODBC connection going through the control panel I cannot
type in the full path to the data files, it doens't allow for enough
characters. The workaround for this (provided by the company who makes the
driver) is to only put a '.' (just the full stop) in the path of the ODBC
connection and set an environment variable, it will then automatically use
that variable as the path. As I said before if I set this for the system it
causes me problems (the ODBC driver is just an add on to a set of other
programs, if the env var is set the programs will also use this as the path,
which is fine unless you have more that one instance of the program using
different data paths - you set the env var for the system and they ALL start
using the same data path, big problem).

Any other suggestions? anyone?

be much appreciated, Gav
 
Hi Gav,

There is a managed way to do it with .Net 2.0 and an unmanaged way to
do it through .Net 1.1 (both are below)

In Visual Studio 2005 Whidbey :
Environment.SetEnvironmentVarilable.

In Visual Studio 2002-3 :
P/Invoke to SetEnvironmentVariable

[DllImport("Kernel32.dll")]
public static extern int SetEnvironmentVariable( string name , string
value ) ;

In 1.1, you can always use WMI (but that is an overkill for only this
little thing).

Regards,
Vaibhav
 
Back
Top