0

我正在创建一个安装项目,在安装过程中,我应该在 My Documents 文件夹中创建一个文件,我在 Commit 方法中的代码如下所示:

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");
        }

这很好用。但是,如果我将我的文档重定向到另一个位置,例如 D:\TestFolder,则代码无法在我的新位置创建文件,并且安装成功完成。任何人都可以帮忙吗?

编辑:我发现 Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) 返回一个错误的值。在控制台应用程序中,这会返回一个正确的值,但在安装项目中,它会返回一个空字符串。

4

0 回答 0