I'm creating a setup project, in the installation, I should create a file in My Documents folder, my code in Commit method looks like this:
base.Commit(savedState);
string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\MyApp";
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\MyApp\Backup.xml";
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
if (!File.Exists(path))
{
File.Create(path).Close();
}
using (TextWriter file = new StreamWriter(path, true))
{
file.WriteLine("adfasdfafdasdfaf");
}
This works well. But if I redirect My Documents to another location, such as D:\TestFolder, the code couldn't create the file in my new location, and the installation complete successfully. Anyone can help?
Edit: I found Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) return me a wrong value. In Console Application, this return me a correct value, but in setup project, it return me an empty string.