0

My original related question here (https://stackoverflow.com/questions/17332403/is-there-such-a-c-sharp-method-or-methodology-that-would-equate-to-filealready) was marked as a duplicate, and I used the supposed duplicate (Is there a way to check if a file is in use?) to try to solve my problem, but I'm still getting flooded with Null Reference Exceptions on some file I/O operations.

Based on that halcyon post of yore, I altered the previous code from this:

public FileQuickRead(string filename)
{
    try
    {
        SR = File.OpenText(filename);
    }
    catch (Exception ex)
    {
        CCR.ExceptionHandler(ex, "FileQuickRead.FileQuickRead");
    }
    . . .

...to this:

public FileQuickRead(string filename)
{
    // Added 6/27/2013; adapted from https://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use
    try
    {
        using (Stream stream = new FileStream(filename, FileMode.Open))
        {
            try
            {
                SR = File.OpenText(filename);
            }
            catch (Exception ex)
            {
                CCR.ExceptionHandler(ex, "FileQuickRead.FileQuickRead");        
            }
        }
    }  
    catch (Exception exc)
    {
        // If the "using Stream filename" fails
        #if TRACE
            UtilCE.LogInfo.Add2LogFile(string.Format("Catch block in FileQuickRead caught: {0}", exc.Message));
        #endif
    }
}

...The log file never contains the "Catch Block" string, so it's getting past the "using filename" but apparently failing on the call to File.OpenText().

Also, it's failing on the other two methods in the class, namely ReadLn and FileClose:

public string FileReadLn()
{
    try
    {
        aLine = SR.ReadLine();
    }
    catch (Exception ex)
    {
        CCR.ExceptionHandler(ex, "FileQuickRead.FileReadLn");
    }
    return aLine;
}

public void FileClose()
{
    try
    {
        SR.Close();
    }
    catch (Exception ex)
    {
        CCR.ExceptionHandler(ex, "FileQuickRead.FileClose");
    }
}

I get a NullReferenceException on FileQuickRead, FileReadLn, then FileClose, three times in succession.

The only other thing in the class are these global declarations:

private StreamReader SR;
private string aLine;

Callers do so in this way:

fileQuickRead = new FileQuickRead(fn);
// Read the line from the file*
aLine = fileQuickRead.FileReadLn();
. . .
while ((aLine != null) && (aLine != ""))
. . .
    aLine = fileQuickRead.FileReadLn();
    if (aLine == null)
        continue;
. . .
    finally
    {
        fileQuickRead.FileClose();
    }

Is the SR.Close() in the FileClose() method not enough? Do I need to do something to completely flush the file, or...???

  • Great - the only comment in the whole project, and it only divulges the achingly obvious.
Community
  • 1
  • 1
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • I think we have an XY problem here (i.e. answering your question won't solve your problem). The code you've posted in this and the previous question just doesn't make sense. Can you step back and tell us what your `FileQuickRead` class is supposed to do? Because from what I see it looks like you're putting an unnecessary and confusing wrapper around `StreamReader`. – Jim Mischel Jun 28 '13 at 19:27
  • I would tell you if I could, but this code is not mine; that's the whole problem with maintaining it - I have no clue what the "architect" had in mind. There is lots of obfuscation, abstraction, delegates, background threads that take no note of other background threads, etc. I've seen bad code before, but this takes the stale cake. That's why I added the comment about the comment (it's not my code; if it were, I would become a...I don't know what, but I'd get out of programming and become a psychiatrist or something. – B. Clay Shannon-B. Crow Raven Jun 28 '13 at 20:30
  • Or another answer to that would be: I could tell you, but then I'd have to kill myself, because if I could understand this mess, (etc.) – B. Clay Shannon-B. Crow Raven Jun 28 '13 at 20:32
  • 1
    If you can't explain what you're trying to accomplish, then I don't see how we can give you any advice about how to accomplish it. – Jim Mischel Jun 28 '13 at 21:10

0 Answers0