Reputation: 1013
I have a program that basically create a CSV file from a database. Below is the code that I use
public void ExportToCSV(string row, int rowNo)
{
FileStream aFile;
try
{
if (rowNo == 0)
aFile = new FileStream(@"..\..\Check.txt", FileMode.Create);
else
aFile = new FileStream(@"..\..\Check.txt", FileMode.Append);
StreamWriter sw = new StreamWriter(aFile);
sw.WriteLine(row);
sw.Close();
}
catch (IOException ex)
{
MessageBox.Show("An IO exception has been thrown!");
MessageBox.Show(ex.ToString());
}
}
The code works ok if I run it from VS2010. The CSV file is created under the program folder. Now I want to be able to install this program to other PCs, so I use ClickOnce. Now with ClickOnce, what I don't understand is it seems that my program does not have its own folder but just a shortcut on my startmenu, but the program runs ok. The confusing thing is I cannot find where the output file is. In one of the XP station, I was able to find it using a search but it is inside some random folder but I cannot find it in Windows 7. Can anyone give me a suggestion what am I doing wrong? Or how I can change the program so it is easily deployed but I can still find where the file is?
Upvotes: 0
Views: 155
Reputation: 216293
The best way to handle this problem is to have a preconfigured path in your app.config.
This path could be inside a well known folder described in the Environment.SpecialFolder enum
At runtime you get the full path of your folder in this way
string myFolderName = Properties.Settings.Default.OutputFolder;
string documentsFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string finalPath = Path.Combine(documentsFolder, myFolderName);
if (rowNo == 0)
aFile = new FileStream(Path.Combine(finalPath, "Check.txt", FileMode.Create);
else
aFile = new FileStream(Path.Combine(finalPath, "Check.txt", , FileMode.Append);
See here for refs on working with Settings
Upvotes: 3