C# 在VS2019中放置txt文件的位置

C# 在VS2019中放置txt文件的位置,c#,visual-studio,inno-setup,C#,Visual Studio,Inno Setup,我在VS2019中有一个c#脚本,现在我想将值写入一个文件,但我必须将.txt文件放在哪里?之后,我想用Inno Setup编译器创建一个Setup.exe 因此,我的问题是,在编程过程中,以及在从Setup.exe安装.exe之后,我应该将文件放在哪里才能访问它 我听说了一些关于这件事,但我不知道这是怎么回事: string filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Applicati

我在VS2019中有一个c#脚本,现在我想将值写入一个文件,但我必须将.txt文件放在哪里?之后,我想用Inno Setup编译器创建一个Setup.exe

因此,我的问题是,在编程过程中,以及在从Setup.exe安装.exe之后,我应该将文件放在哪里才能访问它

我听说了一些关于这件事,但我不知道这是怎么回事:

string filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "combinations.txt");
现在,我在函数顶部设置了权限,正如前面提到的,但我仍然遇到了问题

[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    private void button4_Click(object sender, EventArgs e)
    {
        string date = DateTime.Now.ToString("g");
        string exePath = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
        string logFile = "combinations.txt";

        if (File.Exists(Path.Combine(exePath, logFile)) == false)
            File.Create(Path.Combine(exePath, logFile));

        using (StreamWriter wr = File.AppendText(Path.Combine(exePath, logFile)))
        {
            wr.WriteLine(date + "|" + date);
            wr.Close();
        }
    }

感谢您的帮助

我认为您可以正确使用此路径 它在exe工作的文件夹中

例如,在exe文件夹中写入日志文件。 在调试模式下,它位于调试文件夹中

public static void LogWrite(string mes)
        {
            string exepath= Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location); 
            DateTime _dt = DateTime.Now;
            string logFile= "Log.Inc";
                  
            if (File.Exists(Path.Combine(exepath, logFile)) == false)
                File.Create(Path.Combine(exepath, logFile));

            using (StreamWriter wr = File.AppendText(Path.Combine(exepath, logFile)))
            {
                wr.WriteLine(_dt.ToString("dd/MM/yyyy") + "|" + _dt.ToString("hh:mm") + "|" + mes);
                wr.Close();
            }
        }

根据我的测试,您可以在发布txt文件时尝试使用以下代码将信息记录到txt文件中

private void button1_Click(object sender, EventArgs e)
        {
            string date = DateTime.Now.ToString("g");
            string exePath = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
            string logFile = "combinations.txt";

            using (StreamWriter wr = new StreamWriter(Path.Combine(exePath, logFile), true))
            {
                wr.WriteLine(date + "|" + date);
                wr.Close();
            }
            string path = Path.Combine(exePath, logFile);
            MessageBox.Show(path);
        }
如果使用上述代码,则无需检查文件是否已创建。
另外,我在最后的代码中显示了txt文件的路径。

您是否尝试查看文档以了解该行代码?在这里为您介绍,通常在exe所在的同一文件夹中写入文件在调试时工作正常,在部署时失败。该文件夹需要具有正确的权限,而某些预定义文件夹(如“Program Files”)则不是这种情况,多亏了它的工作,但我必须在Inno Setup Complier中添加该文件吗?还是自动添加到.exe中?我喜欢你的案例;我以前没有尝试过,但找到了一个关于它的话题,也没有尝试解决方案。在你的例子中,看看@mcbabo,如果它在安装后不存在于你的exe路径中,它将自己创建。但是史蒂夫的案子;关于permission.System.UnauthorizedAccessException,我给出的方法可能会出错:对路径“C:\Program Files(x86)\Lotto\combinations.txt”的访问被拒绝。在Vs debug中仍然可以得到错误,但在使用Inno编译后,我得到了以下错误:System.UnauthorizedAccessException:对路径“C:\Program Files(x86)\My Program\combinations.txt”的访问被拒绝。@mcbabo,你试过我的代码吗?因为我没有遇到你提到的例外。我的路径是“C:\Users\username\AppData\Local\Apps\2.0\TK36Y70O.P6H\DEQ0P9YH.GOD\test..tion_37468889c7e228bf_0001.0000_367029bb9a351021\combines.txt”。你可以再查一遍。顺便说一句,我使用visual studio发布程序。当我运行.exe时,它工作正常,但当我执行安装程序并从安装程序中运行.exe时,我得到了错误