Define virtual system variables for children applications [duplicate] - c#

This question already has answers here:
Set environment variables for a process
(2 answers)
Closed 7 years ago.
I am currently working on a program that would run other application under given virtual environment. I am running another application like so:
Process app = new Process();
app.StartInfo.FileName = #"W:\path\to\app\some.exe";
app.EnableRaisingEvents = true;
app.Start();
Now I have faced an issue to change some system variables for application to be run. I have googled for this and could not found the solution. Anybody has any idea how to solve this problem, please help me?
Thanks.
Update
For example I want to set another PATH, JAVA_HOME, AppData variable for child application. Application could be: Google Chrome, Notepad++ or simple .bat script for command line.

assuming you need to change path variable
string pathvariable=Environment.GetEnvironmentVariable("Path");
Environment.SetEnvironmentVariable("Path",pathvariable+";"+"*you new value of path variable*");
if your exe file works fine when you access it directly then you dont need to change the your system variable.. if you change your working directory while accessing the exe file then i guess it will work

Related

Creating DirectoryInfo object for drive root [duplicate]

This question already has answers here:
c# why when the path is "C:" the directoryInfo takes me to the application folder?
(4 answers)
Closed 5 years ago.
In a C# program I am creating an instance of DirectoryInfo. Normally it does not seem to require a trailing slash after a directory name. But if I pass in "C:", rather than getting the root directory for my hard drive I get the directory where my executable is! This certainly seems like a bug but is there some hidden behavior that I am missing?
It isn't explicitly called out in the documentation, but using just (drive): isn't listed as a valid path specification among those that are listed.
The behavior you are seeing is as implemented though, as you can see from the .NET sources:
http://referencesource.microsoft.com/#mscorlib/system/io/directoryinfo.cs,90
The Init method (called from the constructor) does a check for this case, and if it finds it, uses the current working directory (".") instead. Depending on how you launched the EXE, the current working directory could be the location of the EXE.

How do i find file paths in c# windows application? [duplicate]

This question already has answers here:
Loading image from code using relative path in Windows Forms
(2 answers)
Closed 5 years ago.
So, in my program i have a folder called resources, this stores images which i load in the program. Currently i have the path hard-coded in but my friends now want to use the program, is there any way to get the file path to this folder no matter which computer it's on? surely there must be a way.
The current file path is:
H:\Desktop\Solutions\Home\PokeSheet\PokeSheet\Resources
I want it to be able to find the H:\Desktop\Solutions\Home part on its own as that is the part that will change each time
To do this you can use Directory class:
Directory.GetCurrentDirectory();
Or:
// As suggested by Martin Bäckström
AppDomain.CurrentDomain.BaseDirectory;
These will return the directory where program is executed. You need only to concat Resources string to obtained path.
Directory.GetCurrentDirectory(); changed if you open a FileDialog. To solve this you need to do this:
yourDialog.RestoreDirectory = true;
Anyway, to use Resources the best way is:
Resource.YourResource;
Where Resource is the name of you resource file/class.
You can find application startup path in StartupPath static variable:
System.Windows.Forms.Application.StartupPath;

Launching Windows Forms via shortcut (.lnk) causes undesirable result [duplicate]

This question already has answers here:
get path for my .exe [duplicate]
(4 answers)
Closed 5 years ago.
I am using a .txt file to save data in a Windows Forms Application. The .txt is located at the same folder as the .exe. However, if I launch the app via shortcut (let's say a desktop shortcut), the app will save the .txt file in the desktop (even though the actual .exe is located somewhere else). The code I use is:
var myFile = File.Create(#"data.txt");
using (var sw = new StreamWriter(#"data.txt", true))
{
sw.WriteLine("I like apples.");
}
If you right click the shortcut and click the Properties link - You will see an option to change the Start In: path - this is where the programs CWD will be...

How to get Application data folder? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can i get the path of the current user’s “Application Data” folder?
Windows XP Application Data Folder?
I have to save some settings in application data but, when i use something as "#C:\Documents ..." someone can run windous on D:\ So how to get that directory ??
You can use Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); ...
And there's exaple, how you can use it:
string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
and it returns something like C:\\Users\\UserName\\ApplicationData
and you can use Environment.SpecialFolder.Desktop too so you can get to desktop of actual user...
see the code at the end of this:
http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx
Look at this MSDN entry to get the application data directory, Environment.SpecialFolder.
What I used to do is use Evironment.SystemDirectory and then break that down depending on what I need. But if you are worried about the drives then use the DriveInfo class by doing DriveInfo.GetDrives()

C# equivalent of cd C:\appFolder c:\app.exe? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
.Net Process.Start default directory?
I have a C# application, mono to be specific. This application needs to launch another application on the users system. I know of Process.Start and how to use it, but theres something pecuilar about this instance which makes that not work correctly.
For some reason the program I am trying to launch via Process.Start needs to be called from the directory it resides in, otherwise it gives an error on opening.
What I mean by that is, if I open up a command prompt and type in:
C:\appFolder\app.exe
The application will then give me an error.
However if I open a prompt and go:
cd c:\appFolder
app.exe
It then launches just fine.
The problem I am having with process.start is it tries to open the application without first doing what is the equivalent of 'cd c:\appFolder', and so the application gives an error on opening.
So how can I do make Process.Start do what would be the equivalent of first navigating to the apps folder 'cd c:\appFolder' and then calling app.exe?
BTW, I have solved this problem by putting
cd C:\appFolder
app.exe
into a .bat file, and have Process.Start open the .bat file, which works just fine. But I am curious to know if there is a way to cut out the .bat file.
Using cd blah just changes your working directory. You can set the working directory of your process by setting the WorkingDirectory of your ProcessStartInfo. Perhaps something like this:
var procInfo = new ProcessStartInfo("app.exe");
procInfo.WorkingDirectory = #"C:\appFolder";
Process.Start(procInfo);
try changing the working directory before your call
Directory.SetCurrentDirectory(#"path");
http://msdn.microsoft.com/en-us/library/system.io.directory.setcurrentdirectory.aspx
var psi = new ProcessStartInfo("app.exe");
psi.WorkingDirectory = #"C:\appFolder";
Process.Start(psi);
Use a ProcessStartInfo object to start the application and set the WorkingDirectory property accordingly.

Categories