473,508 Members | 4,324 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Append to PATH Environment Variable

I have an application that uses PInvoke to call into native DLLs. My
DLLimport attribute only has the name of the DLL that I need to import (i.e.
I leave it up to the OS to search the PATH for the DLL.) This all works
great when the user's PATH variable is setup correctly.

Unfortunately, there are times when the user's PATH is too full or whatever
to have the correct search path for my application.

What I was wondering is if there is a way to set the PATH variable to have
the correct search directory after my application starts up (yet before any
call to DLLImport). This way I can guarantee that the search is correct for
the running instace of my application.

I tried this at the beginning of my application to no avail:

System.Diagnostics.ProcessStartInfo info =
System.Diagnostics.Process.GetCurrentProcess().Sta rtInfo;

string origpath = info.EnvironmentVariables["path"];
string newpath = "C:\\mypath;" + origpath;
info.EnvironmentVariables["path"] = newpath;

****

I know I could create a separate executable that sets the Environment
variables and calls StartProcess to launch my application. I was hoping that
my application could set this path infromation without requiring another
executable to launch my application.

Thanks!
Nov 16 '05 #1
1 9661
Chris,

You could always call the SetEnvironmentVariable function through the
P/Invoke layer.

Also, if you can wait for .NET 2.0, you are able to take a function
pointer and assign it to a delegate to call. This will allow you to load
the dll (LoadLibrary, LoadLibraryEx), get the procedure address
(GetProcAddress), and then get a delegate that you can call which will call
the function at that address.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Chris B" <Ch****@discussions.microsoft.com> wrote in message
news:F7**********************************@microsof t.com...
I have an application that uses PInvoke to call into native DLLs. My
DLLimport attribute only has the name of the DLL that I need to import
(i.e.
I leave it up to the OS to search the PATH for the DLL.) This all works
great when the user's PATH variable is setup correctly.

Unfortunately, there are times when the user's PATH is too full or
whatever
to have the correct search path for my application.

What I was wondering is if there is a way to set the PATH variable to have
the correct search directory after my application starts up (yet before
any
call to DLLImport). This way I can guarantee that the search is correct
for
the running instace of my application.

I tried this at the beginning of my application to no avail:

System.Diagnostics.ProcessStartInfo info =
System.Diagnostics.Process.GetCurrentProcess().Sta rtInfo;

string origpath = info.EnvironmentVariables["path"];
string newpath = "C:\\mypath;" + origpath;
info.EnvironmentVariables["path"] = newpath;

****

I know I could create a separate executable that sets the Environment
variables and calls StartProcess to launch my application. I was hoping
that
my application could set this path infromation without requiring another
executable to launch my application.

Thanks!

Nov 16 '05 #2

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

Similar topics

3
24828
by: Jeff Wagner | last post by:
Is it possible to append a new Path to some file permanently? It seems like a sys.path.append('/my/new/path') statement is temporary. In other words, where and in what file on a Win32 box or...
1
2222
by: jwpioneer | last post by:
I have a need within an application to modify the path environment variable, as I need to find specific directories and remove them. I use the following code to do this: RegistryKey rkey = ...
1
2446
by: Bonj | last post by:
Hi My application installs a front-end GUI, which runs code when buttons are clicked. It also installs a command-line utility, that is a console application. They are both installed to the...
6
6405
by: Garfield | last post by:
We have written a small function to append a couple of paths to the path environmental variable. The code is: string PathValue = ""; string sAdd = ""; sAdd = ";" + <path1> + "Bin;" +...
10
3052
by: mirandacascade | last post by:
O/S: Win2K Vsn of Python:2.4 Based on a search of other posts in this group, it appears as though os.environ is one way to obtain the PATH environment variable. My questions: 1) is it...
3
3880
by: HMS Surprise | last post by:
Is sys.path setup differnently in jython vs python? I have environment variables pythonpath and jythonpath set to include C:\python22 but the initial printout indicates it is being ignored. Also...
3
2556
by: Wolfgang Meister | last post by:
I would like to append text to a file which might exist or not. What is the easiest way to open or create it depending if it exists or not and to set it into an "append" text mode?
0
1768
by: cnathaide | last post by:
Hi, I have written a simple command line application in C# and created the accompanying setup project in VS 2005. When the user installs the application on the his/her machine, I would like...
2
1424
by: Blair | last post by:
Hello, I have a old enviromantal path in LIB file that, every time I compile gives a error message. It does not stop the compiler just gives a warning. This has become very annoying. I...
0
7228
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,...
0
7128
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...
0
7393
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...
1
7058
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...
0
5635
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,...
0
3206
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...
0
3191
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
769
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
426
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.