检查正确文件夹中的 .config 文件
我用 C# 编写了一个程序,当我打开 nfo 文件时,它会读取文本文件(.nfo 文件)。 (将 .nfo 文件与我的 exe 文件相关联)。
该程序会检查配置文件,如果丢失,则会创建一个新配置文件。
如果我手动运行 exe 文件,然后从程序内部打开 nfo 文件,则效果很好。当我双击 nfo 文件时,程序启动,但也会在与 nfo 文件相同的文件夹中写入配置文件。
我希望程序仅检查与 exe 文件位于同一文件夹中的配置文件。我希望 exe 文件也独立于文件夹。
这是配置检查/写入的代码:
string configFil = "XnfoReader.exe.Config";
StreamWriter sw = null;
string configText = @"<?xml version=""1.0"" encoding=""utf-8"" ?>"
if (!File.Exists(configFil))
{
FileStream fs = File.Open(configFil, FileMode.CreateNew, FileAccess.Write);
sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
sw.WriteLine(configText);
sw.Close();
sw = null;
}
我在这里有意义吗?
I have made a program in C# that reads text files (.nfo files) when I open the nfo file. (Associated .nfo files with my exe file).
The program checks for a config file, and if it's missing, it creates a new one.
This works fine if I run the exe file manualy, and then open an nfo file from inside the program. When I double click an nfo file, the program starts, but allso writes a config file in the same folder as the nfo file.
I want the program to ONLY check for config file in the same folder as the exe file. I want the exe file to be folder independent too.
Here's the code for the config check / write:
string configFil = "XnfoReader.exe.Config";
StreamWriter sw = null;
string configText = @"<?xml version=""1.0"" encoding=""utf-8"" ?>"
if (!File.Exists(configFil))
{
FileStream fs = File.Open(configFil, FileMode.CreateNew, FileAccess.Write);
sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
sw.WriteLine(configText);
sw.Close();
sw = null;
}
Am I making sense here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案非常简单(对你来说很幸运),但并不是很明显。实际上,您只输入配置文件的名称,因此应用程序添加当前路径以获取配置文件的完整路径。
这是您的问题,因为当您之前在其他位置打开 nfo 文件时,当前路径成为 nfo 文件的路径。
所以解决方案是直接将完整路径放入您的配置路径,为此您可以使用:
The solution is pretty simple -lucky to you- but not really obvious. Actually you only put the name of your config file, so the application add the current path to get the full path to the config file.
That is your problem, because as you previously open an nfo file in an other location, the current path became the path to the nfo file.
So the solution is to put directly the full path to your config path, to do that you can use :