C# 将文件写入任何计算机上的项目文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19329672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:46:16  来源:igfitidea点击:

Write file to project folder on any computer

c#file-ioio

提问by blutuu

I'm working on a project for a class. What I have to do is export parsed instructions to a file. Microsoft has this example which explains how to write to a file:

我正在为一个班级做一个项目。我要做的是将解析后的指令导出到文件中。微软有这个例子解释了如何写入文件:

// Compose a string that consists of three lines.
string lines = "First line.\r\nSecond line.\r\nThird line.";

// Write the string to a file.
System.IO.StreamWriter file = new System.IO.StreamWriter("c:\test.txt");
file.WriteLine(lines);

file.Close();

I'm fine with that part, but is there a way to write the file to the current project's environment/location? I'd like to do that instead of hard coding a specific path (i.e. "C:\\test.txt").

我对那部分没问题,但是有没有办法将文件写入当前项目的环境/位置?我想这样做而不是硬编码特定路径(即"C:\\test.txt")。

采纳答案by evanmcdonnal

Yes, just use a relative path. If you use @".\test.txt"( btw the @ just says I'm doing a string literal, it removes the need for the escape character so you could also do ".\\test.txt"and it would write to the same place) it will write the file to the current working directory which in most cases is the folder containing your program.

是的,只需使用相对路径。如果您使用@".\test.txt"(顺便说一句,@ 只是说我正在做一个字符串文字,它消除了对转义字符的需要,因此您也可以这样做".\\test.txt"并且它会写入同一个地方)它将文件写入当前工作目录在大多数情况下是包含您的程序的文件夹。

回答by xxbbcc

You can use Assembly.GetExecutingAssembly().Locationto get the path of your main assembly (.exe). Do note that if that path is inside a protected folder (for example Program Files) you won't be able to write there unless the user is an administrator - don't rely on this.

您可以使用Assembly.GetExecutingAssembly().Location来获取主程序集 (.exe) 的路径。请注意,如果该路径在受保护的文件夹内(例如Program Files),除非用户是管理员,否则您将无法在那里写入 - 不要依赖于此。

Here is sample code:

这是示例代码:

string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
string fileName = Path.Combine(path, "test.txt");

This question / answershows how to get the user's profile folder where you'll have write access. Alternatively, you can use the user's My Documentsfolder to save files - again, you're guaranteed to have access to it. You can get that path by calling

此问题/答案显示了如何获取您将拥有写入权限的用户配置文件文件夹。或者,您可以使用用户的My Documents文件夹来保存文件 - 同样,您可以保证可以访问它。您可以通过调用获得该路径

Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

回答by Ahmed Soliman

If you want to get the current folder location of your program use this code :

如果要获取程序的当前文件夹位置,请使用以下代码:

string path = Directory.GetParent(System.Reflection.Assembly.GetExecutingAssembly().Location).FullName; // return the application.exe current folder
string fileName = Path.Combine(path, "test.txt"); // make the full path as folder/test.text

Full code to write the data to the file :

将数据写入文件的完整代码:

string path = Directory.GetParent(System.Reflection.Assembly.GetExecutingAssembly().Location).FullName;
string fileName = Path.Combine(path, "test.txt");

if (!File.Exists(fileName))
{
    // Create the file.
    using (FileStream fs = File.Create(fileName))
    {
        Byte[] info =
            new UTF8Encoding(true).GetBytes("This is some text in the file.");

        // Add some information to the file.
        fs.Write(info, 0, info.Length);
    }
}