简体   繁体   English

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

[英]C# - Saving a '.txt' File to the Project Root

I have written some code which requires me to save a text file.我写了一些代码,需要我保存一个文本文件。 However, I need to get it to save to my project root so anyone can access it, not just me.但是,我需要将它保存到我的项目根目录,以便任何人都可以访问它,而不仅仅是我。

Here's the method in question:这是有问题的方法:

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

Many people told me that using AppDomain.CurrentDomain.BaseDirectory would contain the dynamic location of where the app was stored.许多人告诉我,使用AppDomain.CurrentDomain.BaseDirectory将包含应用程序存储位置的动态位置。 However, when I execute this, nothing happens and no file is created.但是,当我执行此操作时,没有任何反应,也没有创建文件。

Is there another way of doing this, or am I just using it completely wrong?有没有其他方法可以做到这一点,还是我只是完全错误地使用它?

File.WriteAllText requires two parameters: File.WriteAllText需要两个参数:
The first one is the FileName and the second is the content to write第一个是文件名,第二个是要写入的内容

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

Keep in mind however that writing to the current folder could be problematic if the user running your application has no permission to access the folder.但是请记住,如果运行您的应用程序的用户无权访问该文件夹,则写入当前文件夹可能会出现问题。 (And in latest OS writing to the Program Files is very limited). (并且在最新的操作系统中写入程序文件非常有限)。 If it is possible change this location to the ones defined in Environment.SpecialFolder enum如果可以将此位置更改为Environment.SpecialFolder枚举中定义的位置

I wish also to suggest using the System.IO.Path class when you need to build paths and not a string concatenation where you use the very 'OS specific' constant "\\" to separate paths.我还建议在您需要构建路径时使用System.IO.Path 类,而不是使用“操作系统特定”常量"\\"来分隔路径的字符串连接。

In your example I would write在你的例子中,我会写

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

no need for the extra + @"\\" just do:不需要额外的+ @"\\"只需执行以下操作:

AppDomain.CurrentDomain.BaseDirectory + fileName

and replace the parameters并替换参数

saveScene.ToString()

and

AppDomain.CurrentDomain.BaseDirectory + fileName

your code should be:你的代码应该是:

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

you can read on File.WriteAllText here :你可以在这里阅读File.WriteAllText

Parameters参数

 path Type: System.String The file to write to. contents Type: System.String The string to write to the file.

Instead of using AppDomain.CurrentDomain.BaseDirectory you can just do it this way:而不是使用AppDomain.CurrentDomain.BaseDirectory你可以这样做:

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

When you don't add anything, the basedirectory is automatically assumed.当您不添加任何内容时,将自动假定基目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM