skip to Main Content

bashing my head against the wall with this one, tried adding an app manifest and elevating, but whenever my program tries to read c:usersusernameappdataroamingmicrosoftteamslogs.txt
it throws an exception.
If I use PowerShell, I can read this file with no permissions issues.

Edit – what is interesting is if I compare the security of two folders at the same level, s ay the Excel Folder and the teams folder under /appdata/roaming/Microsoft. Both are identical, and I can access the Excel one but not the teams one. Even Elevating does not help. Yet outside of Visual Studio, I can browse and access these paths quite happily as a user..

Here is the code snippet:

        private static string GetTeamsLog()
    {
        string homeDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
        // Code to read the log files and return the contents as a string
        // Path to the log file(s)
        string logPath = homeDir + @"AppDataRoamingMicrosoftTeams";
        string logFile = logPath + "logs.txt";
        string log = "";
        try
        {
            if (!File.Exists(logFile))
            {
                throw new FileNotFoundException("The log file was not found.");
            }

            if ((File.GetAttributes(logFile) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
            {
                throw new UnauthorizedAccessException("The application doesn't have the necessary permissions to access the file.");
            }

            if (Path.GetFullPath(logFile).Length > 260)
            {
                throw new PathTooLongException("The specified path is too long.");
            }

            if (!DriveInfo.GetDrives().FirstOrDefault(x => x.RootDirectory.Name == Path.GetPathRoot(logFile)).IsReady)
            {
                throw new DriveNotFoundException("The specified drive is not valid or not ready.");
            }

            // Read the contents of the log file
            log = System.IO.File.ReadAllText(logFile);
        }
        catch (FileNotFoundException e)
        {
            Console.WriteLine(e.Message);
        }
        catch (UnauthorizedAccessException e)
        {
            Console.WriteLine(e.Message);
        }
        return log;
    }

The error triggered by log = System.IO.File.ReadAllText(logfile); is

System.UnauthorizedAccessException: 'Access to the path 'C:UsersjimmyAppDataRoamingMicrosoftTeamslogs.txt' is denied.'
This exception was originally thrown at this call stack:
        Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(string, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, System.IO.FileOptions)
        Microsoft.Win32.SafeHandles.SafeFileHandle.Open(string, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, System.IO.FileOptions, long, System.IO.UnixFileMode?)
        System.IO.Strategies.OSFileStreamStrategy.OSFileStreamStrategy(string, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, System.IO.FileOptions, long, System.IO.UnixFileMode?)
        System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(string, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, System.IO.FileOptions, long, System.IO.UnixFileMode?)
        System.IO.StreamReader.ValidateArgsAndOpenPath(string, System.Text.Encoding, int)
        System.IO.File.ReadAllText(string, System.Text.Encoding)
        TeamsMonitor.Form1.GetTeamsLog() in Form1.cs

2

Answers


  1. Chosen as BEST ANSWER

    For completeness, it appears Acronis Cyber Protect was messing with the permissions. Even though the "protection" was disabled, the Acronis Cyber Protect service was still running. Once killed, I no longer had any issues.


  2. It might be used by Teams. So read it this way:

    using(FileStream logFileStream = new FileStream(logFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    {
        using(StreamReader logFileReader = new StreamReader(logFileStream))
        {
            string text = logFileReader.ReadToEnd();
            // Your code..
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search