Search code examples
c#windows-cestreamreaderfileinfodirectoryinfo

Why am I not getting the text from my file with this code?


I am trying to load all my "error log files" into one (hopefully not gigantic) string, and then assign that string to a Multiline textBox:

private void PopulateTextBox()
{
    const string errLogDir = "\\ErrorLog";
    Directory.CreateDirectory(errLogDir);
    StringBuilder sb = new StringBuilder();
    DirectoryInfo di = new DirectoryInfo(errLogDir);
    FileInfo[] files = di.GetFiles("*.txt");
    foreach (FileInfo fi in files)
    {
        sb.AppendLine(String.Format("*** {0} ***", fi.FullName));
        using (StreamReader sr = new StreamReader(fi.FullName))
        {
            String line;
            while ((line = sr.ReadLine()) != null)
            {
                sb.AppendLine(line);
            }
        }
        sb.AppendLine(String.Empty);
    }

    string errLogContents = sb.ToString();
    MessageBox.Show(String.Format("errLogContents is {0}", errLogContents));
    textBoxErrLogsContents.Text = sb.ToString();
}

There is one file (named "ExceptionLog_2.11.2009.txt"), with some text data, in the \ErrorLog directory directly below the .exe directory (files are created programatically when exceptions occur).

So why am I seeing an empty string in the MessageBox.Show() and nothing is in textBoxErrLogsContents?

UPDATE

Virtually the same code works in a VS 2013 "Sandbox" app, with just a different directory name, file type, and textBox name:

const string errLogDir = @"C:\MiscInWindows7";
Directory.CreateDirectory(errLogDir);
StringBuilder sb = new StringBuilder();
DirectoryInfo di = new DirectoryInfo(errLogDir);
FileInfo[] files = di.GetFiles("*.xml");
foreach (FileInfo fi in files)
{
    sb.AppendLine(String.Format("*** {0} ***", fi.FullName));
    using (StreamReader sr = new StreamReader(fi.FullName))
    {
        String line;
        while ((line = sr.ReadLine()) != null)
        {
            sb.AppendLine(line);
        }
    }
    sb.AppendLine(String.Empty);
}

string errLogContents = sb.ToString();
MessageBox.Show(String.Format("errLogContents is {0}", errLogContents));
textBox7.Text = sb.ToString();

UPDATE 2

ctacke whammed the spike on the noggin re: my not providing the full path. This works:

private void PopulateTextBox()
{
    String logDirPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
    String fullPath = logDirPath + "\\ErrorLog";
    StringBuilder sb = new StringBuilder();
    DirectoryInfo di;

    if (!Directory.Exists(fullPath))
    {
        di = Directory.CreateDirectory(fullPath);
    }
    else
    {
        di = new DirectoryInfo(fullPath);
    }

    FileInfo[] files = di.GetFiles("*.err");
    foreach (FileInfo fi in files)
    {
        sb.AppendLine(String.Format("*** {0} ***", fi.FullName));
        using (StreamReader sr = new StreamReader(fi.FullName))
        {
            String line;
            while ((line = sr.ReadLine()) != null)
            {
                sb.AppendLine(line);
            }
        }
        sb.AppendLine(String.Empty);
    }
    textBoxErrLogsContents.Text = sb.ToString();
}

Solution

  • First, calling CreateDirectory in all cases is a bad idea. It's confusing. Instead, check to see if the directory exists, and if it doesn't then create it and exit the function (because if you created it, you know there's no data). This would have found your error much faster and also prevented @Jim Mischel's (and everyone else's) confusion.

    The problem with your code is actually rooted in one of your comments. You say the file, as seen from your PC with WMDC, is at Computer\WindowsCE\Program Files\HHS\ErrorLog. On the device itself that equates to \Program Files\HHS\ErrorLog not just \ErrorLog. Remember, there are no relative paths on Windows CE devices.