c# - C#我的主函数在发布后找不到路径
问题描述
您好,我的 C# 程序在调试和发布模式下工作正常,但在发布后找不到之前可以找到的文件。这是我的主要功能,它应该从 txt 中检索特殊行,并进一步提供给其他功能。
static void Main()
{
string pathMe = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var pathToServer = File.ReadAllLines(@pathMe + "\\ServerPath.txt");
//Console.WriteLine(pathToServer[2]+"\\ServerPath.txt");
Thread.Sleep(600);
if (File.Exists(Callerlog)) File.Delete(Callerlog);
if (File.Exists(NotAnsweredLogs)) File.Delete(NotAnsweredLogs);
if (File.Exists(TempNoAnswerLog)) File.Delete(TempNoAnswerLog);
if (File.Exists(TempCallerLog)) File.Delete(TempCallerLog);
if (!File.Exists(Callerlog)) File.Copy(pathToServer[2]+ "\\local1.info.log", Callerlog);
//if (!File.Exists(Callerlog)) File.Copy(ServerLogFile, Callerlog);
if (!File.Exists(NotAnsweredLogs)) File.Create(NotAnsweredLogs).Close(); ;
if (!File.Exists(TempNoAnswerLog)) File.Create(TempNoAnswerLog).Close();
if (!File.Exists(TempCallerLog)) File.Create(TempCallerLog).Close() ;
while (true)
{
CheckTime();
CompareTxt();
Thread.Sleep(19000);//change to 19 sec
}
似乎在发布版本中,字符串 pathMe 似乎是空的。errorMessage 看起来像:
Unhandled exception. System.IO.FileNotFoundException: Could not find file 'C:\ServerPath.txt'.
File name: 'C:\ServerPath.txt'
at System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle)
at System.IO.FileStream.CreateFileOpenHandle(FileMode mode, FileShare share, FileOptions options)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
at System.IO.StreamReader.ValidateArgsAndOpenPath(String path, Encoding encoding, Int32 bufferSize)
at System.IO.StreamReader..ctor(String path, Encoding encoding)
at System.IO.File.InternalReadAllLines(String path, Encoding encoding)
at System.IO.File.ReadAllLines(String path)
at CallParser.Program.Main()
我不明白,txt 文件 ServerPath 看起来像这样:
Hier Pfad zum Logfile einfügen (Beispiel ersetzen!!!)
BSP:
C:\\Users\\lukas\\Projekte\\TeamsPlugin\\BSP_ServerLog
因为我认为代码应该从 txt 文件中获取路径,使用它来查找文件并复制它对吗?
解决方案
你应该尝试另一种方法。
例如:
string pathMe = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
您可以在这些 StackOverflow 链接中找到更多信息:
和