我在VS 2019中有一个c#脚本,现在我想将值写入文件,但我必须将.txt文件放在哪里?之后,我想用Inno Setup Compiler制作一个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文件中。
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文件的路径。