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()
Related
This question already has an answer here:
C# relative path not start from working directory
(1 answer)
Closed 3 years ago.
System.Diagnostics.Process.Start("../Shortcuts/slot1.lnk");
This line doesn't work.
I have checked that the file ending is correct and that the file is in the correct directory and yet it still doesn't work.
It should start the shortcut but it just says that it doesn't find the shortcut.
the folder structure is as follows /folder1/Shortcuts/slot1.lnk and the program is /folder1/program/program.exe
I would like to mention that I also load pictures the same way... They work this doesn't
pictureBox1.BackgroundImage = Image.FromFile("../Slots/slot2.jpg");
folder structure for this is the same so /folder1/Slots/slot2.jpg
and the program is /folder1/program/program.exe
I got it working by using double slashes
System.Diagnostics.Process.Start("..\\Shortcuts\\slot1.lnk");
This worked for me:
System.Diagnostics.Process.Start("C:\\Users\\maple\\Desktop\\slot1.lnk");
Note the absolute path, and the backslashes instead of forward slashes.
Once you get it working with absolute path, you can try the relative path, but be aware that generally you don't control what the current directory is when your user starts your application. If you really want to use relative paths, you should explicitly set current directory in your code.
This question already has answers here:
What is the best way to store user settings for a .NET application?
(8 answers)
Closed 5 years ago.
My program must produce files from some given data. I'm dealing with PDFs and Excel documents. How do I allow the user to set the directory where the files will be saved? I'm not referring to SaveFileDialog where the user must choose the directory every time.
I want the files to automatically be saved to the directory previously specified by the user. Something to this effect:
Most immediate solution I can think of is to store the directory in a file and read it every time a file is to be saved.
I also read about Properties.Settings.Default.Save(), but is it relevant to my case?
Use FolderBrowserDialog to get the folder...
https://msdn.microsoft.com/en-us/library/system.windows.forms.folderbrowserdialog(v=vs.110).aspx
Get the folder's path.
folderName = folderBrowserDialog1.SelectedPath;
Then go into your project properties (Project menu > Project Name Properties), and click the settings tab. Add a new setting with a name of your choice, like SaveLocation with the type of string. Then you can save it like so...
Settings.Default["SaveLocation"] = folderName;
Properties.Settings.Default.Save();
And then, obviously, retrieve it like so...
string saveLocation = Settings.Default["SaveLocation"]
Read more about saving application settings here: https://msdn.microsoft.com/en-us/library/a65txexh.aspx
You may care to use the registry to store information between sessions. This will require that you have admin privileges. Since this is winform it may be.
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;
This question already has answers here:
Log4net output to My Documents
(3 answers)
Closed 7 years ago.
I'm looking to place some logfiles in the user's documents folder. The log path for my log4net logger is configured in the App.config XML, so I can't programmatically determine the path using the usual methods. Can the My Documents library be accessed by an environment variable, like pre-Windows 7 documents folders?
Is this what you are looking for?
string path = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
Edit
If you logging other than log4net. You need not store the log file path in the app.config in that case. because whatever you write in app.config you will end up overriding it. So it will be misleading and useless. If you want to pickup the MyDocuments location my suggestion is to fetch it in the code and wire it up with your logging module.
If you are using log4netuse the suggestion by #derpirscher - Log4net output to My Documents
Have a look at Environment.GetFolderPath (MSDN)
Now, as you clarified it's for log4net have a look at the following question
Log4net output to My Documents
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