0

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.

James
  • 2,570
  • 7
  • 34
  • 57
  • did you check permissions? – Masoumeh Karvar Nov 07 '13 at 06:19
  • if it is related to permission, when My Documents isn't redirected to another location, it also can't write file, but it works, only not work when My Documents was redirected to another location – James Nov 07 '13 at 06:36

0 Answers0