![](/img/trans.png)
[英]c# visual studio - find absolute path of image after publishing the project
[英]C# my Main function cant find the path after Publishing
你好,我的 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 链接中找到更多信息:
和
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.