C# - 将 '.txt' 文件保存到项目根目录

15

我写了一些代码需要保存文本文件。然而,我需要让它保存在我的项目根目录,这样任何人都可以访问,而不仅仅是我。

以下是相关的方法:

private void saveFileToolStripMenuItem_Click(object sender, EventArgs e)
    {
        try
        {
            string fileName = Microsoft.VisualBasic.Interaction.InputBox("Please enter a save file name.", "Save Game");
            if (fileName.Equals(""))
            {
                MessageBox.Show("Please enter a valid save file name.");
            }
            else
            {
                fileName = String.Concat(fileName, ".gls");
                MessageBox.Show("Saving to " + fileName);

                System.IO.File.WriteAllText(saveScene.ToString(), AppDomain.CurrentDomain.BaseDirectory + @"\" + fileName);
            }
        }
        catch (Exception f)
        {
            System.Diagnostics.Debug.Write(f);
        }
    }

很多人告诉我使用AppDomain.CurrentDomain.BaseDirectory可以包含应用程序所存储的动态位置。然而,当我执行此操作时,什么也没有发生,也没有创建任何文件。

是否有其他方法可以实现这一点,或者我只是完全错误地使用了它?


你的文件在 debug/bin 位置(运行时的目录)。你没有做错任何事情,只是在错误的位置。 - Matt
3个回答

38

File.WriteAllText 需要两个参数:
第一个是文件名,第二个是要写入的内容。

File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory + @"\" + fileName, 
                  saveScene.ToString());

请注意,如果运行您的应用程序的用户对文件夹没有写权限,则将内容写入当前文件夹可能会有问题(在最新的操作系统中,写入Program Files也受到限制)。如果可能,请更改此位置为Environment.SpecialFolder枚举中定义的位置。

我还建议在需要构建路径而不是使用非常“特定于操作系统”的常量"\"来分隔路径的字符串连接时使用System.IO.Path class

在您的示例中,我会这样写:

 string destPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,fileName);
 File.WriteAllText(destPath, saveScene.ToString());

1
这不是项目文件夹的路径,而是 bin 目录的路径。 - Agent Shoulder
我不理解你的评论。 _bin目录_是什么意思? - Steve
以上解决方案将写入<myProjectFolder>/bin/<configuration>/,而不仅仅是<myProjectFolder>。 - Agent Shoulder
你有一个WinForms/WPF应用程序或Web应用程序吗?你正在使用.NET Core还是Framework? - Steve

5

不需要额外的+ @"\",只需执行以下操作:

AppDomain.CurrentDomain.BaseDirectory + fileName

替换参数

saveScene.ToString()

并且

AppDomain.CurrentDomain.BaseDirectory + fileName

你的代码应该是:

private void saveFileToolStripMenuItem_Click(object sender, EventArgs e)
    {
        try
        {
            string fileName = Microsoft.VisualBasic.Interaction.InputBox("Please enter a save file name.", "Save Game");
            if (fileName.Equals(""))
            {
                MessageBox.Show("Please enter a valid save file name.");
            }
            else
            {
                fileName = String.Concat(fileName, ".gls");
                MessageBox.Show("Saving to " + fileName);

                System.IO.File.WriteAllText(AppDomain.CurrentDomain.BaseDirectory +  fileName, saveScene.ToString());
            }
        }
        catch (Exception f)
        {
            System.Diagnostics.Debug.Write(f);
        }
    }

您可以在此处阅读有关File.WriteAllText的信息:

Parameters

   path Type: System.String 

       The file to write to.  

   contents Type: System.String

       The string to write to the file.

0

不必使用 AppDomain.CurrentDomain.BaseDirectory,你可以这样做:

    File.WriteLine("data\\Mytxt.txt", "Success!");

当您没有添加任何内容时,基目录会自动被假定。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接