簡體   English   中英

C#我的Main函數在發布后找不到路徑

[英]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 鏈接中找到更多信息:

獲取我的 .exe 的路徑

如何在 .NET 控制台應用程序中獲取應用程序的路徑?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM