locked
Reading in text from a live irc chat log file RRS feed

  • Question

  • Hey, 

    So i am trying to write a program in c# that reads in text from a irc chat log file (.txt). This document is located in the following directory: "C:\Users\finnj\AppData\Roaming\HexChat\scrollback\#finnjanssens\#finnjanssens.txt". (#finnjanssens is the irc channel of my twitch livestream chat). I first tried to just read in the text in a single string and print it out to the console. This is the code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    
    namespace TwitchIRC
    {
        class ReadFromFile
        {
            static void Main()
            {
                for (int i = 2; i > 1; i++)
                {
                    string text = File.ReadAllText(@"C:\Users\finnj\AppData\Roaming\HexChat\scrollback\#finnjanssens\#finnjanssens.txt");
                    Console.WriteLine(text);
    
                }
            }
        }
    }
    I then had the problem that the program wasnt authorized to access the path of the log. I solved this by adding a app.manifest item and replacing the following line: 
    <requestedExecutionLevel level="asInvoker" uiAccess="false" />
    with
    <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
    This solved thre problem of the unauthorized access, but now i get a "IOexception was unhandled" error. This is probably because Hexchat (my irc client) is writing to the textdocument at the same time. If i close my irc client it works just fine, but i need my irc client to write to the log, else the program has no purpose. Is there a way to get around this? Thanks!
    Monday, December 21, 2015 6:06 PM

Answers

  • It completely depends upon how the file was opened for writing. If it was opened without read share rights then there is nothing you can do about it.  You cannot read the file until the other process releases it. If the file was open with read share rights then you can read the file without issue. 

    But if there was an IOException then it will tell you why it failed either as part of the message or as an inner exception. You should confirm the fact that the file is locked and not readable.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    • Proposed as answer by Kristin Xie Thursday, December 24, 2015 9:13 AM
    • Marked as answer by Kristin Xie Wednesday, December 30, 2015 2:22 AM
    Monday, December 21, 2015 10:35 PM