0

Hello my C# Programm works fine in debug and release mode but after publishing it cant find the file it could before. This is my main function where it should retrieve a special line from a txt and give it further for other functions.

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


        }

It seems that in publish version the string pathMe seems to be empty. the errorMessage looks like:

    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()

I dont get it and the txt file ServerPath looks like this:

Hier Pfad zum Logfile einfügen (Beispiel ersetzen!!!)
BSP:
C:\\Users\\lukas\\Projekte\\TeamsPlugin\\BSP_ServerLog

As I think the code should get the path from the txt file use it to find the file and copy it right?

GoebloJr
  • 1
  • 1

1 Answers1

0

You shoud try an alternative approach.

For example:

string pathMe = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);

You can find more info in these StackOverflow links:

get path for my .exe

and

How can I get the application's path in a .NET console application?

Kapitany
  • 1,319
  • 1
  • 6
  • 10