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...
Related
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:
Windows 10 Universal App File/Directory Access
(6 answers)
Closed 6 years ago.
I was wondering if anyone would know how to get an Universal Windows app to read a txt file.
StreamReader lezer = new StreamReader(#"C:\Users\LotteDiesveld\Documents\Visual Studio 2015\Projects\Jaar 1\Periode 2\OIS 12\Eindopdracht\Personen.txt");
tbPersonen.Items.Add(lezer.ReadToEnd());
I've also tried this
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync("ms-appx:///reserveringen.txt")
I know multiple answers have been given on this subject, but none work, because I keep getting an error. This error is either "Cannot convert type string to type uri" or "cannnot convert type string to type stream".
A UWP app runs in a sandboxed environment and has no permissions to read a file from anywhere on the hard drive. If you want to read a file in the user's Documents Library you should either let the user select the file using a picker or declare the folder in the application's manifest. Please refer to Jerry Nixon's blog post for more information: http://blog.jerrynixon.com/2012/06/windows-8-how-to-read-files-in-winrt.html
Please also refer to the following similar question:
Windows 10 Universal App File/Directory Access
For information about how to read a file that is distributed along with your app, please see the answer here:
how to read a text file in windows universal app
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
This question already has answers here:
How to associate a file extension to the current executable in C#
(9 answers)
Closed 9 years ago.
I know there are thousands of this type of question on S.O but i haven't found an appropraite solution that suits my problem.
I have a simple text file create creator that saves file using the default extension .tj. I did this using
DialogResult DR = openFile.ShowDialog();
if (DR == DialogResult.OK)
{
StreamWriter writeFile = new StreamWriter(new FileStream(openFile.FileName,FileMode.CreateNew,FileAccess.Write));
writeFile.WriteLine(rtbText.Text);
writeFile.Flush();
writeFile.Close();
}
This works perfectly but what i want is that whenever my Application is installed and a file with the default extension is saved, Whenever the file is Double Clicked, the file opens in my Application and the Rich Text Box shows the text in the file.
Any help will be appreciated
If you're using click once deployment, there is a way. You can specify file type associations in your installer and these will be put on the target machine for you. Once they launch a file with that association, your app will start. Taken from this MSDN article. If you're using Nullsoft's NSIS installers, you can do something similar, taken from this file type association article
From here, take the answer from this question to grab the string of the file. Then use a StreamReader or similar to read the file's contents and put them in to the rich text box. Voila!
Hope this helps and let me know if you need any clarification.