Nana
Nana

Reputation: 155

Directory not found exception

I am trying to write an error log which will store down all errors into a .txt file. But right now, the problem im facing is "Directory Not Found Exception"

Below is my code for my common method (writelog)

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using System.Diagnostics;

/// <summary>
/// Summary description for Writelog
/// </summary>
/// <param name="ErrorDesc">Desc</param>
/// <param name="ID">ID</param>
/// <param name="ProgPage">Name</param>
/// <param name="Message">Error Message</param>
public class Writelog
{
    public static void WritelogDesc(string Desc, string ID, string Name, string ErrorMessage)
    {
        StringBuilder sBuilder = new StringBuilder();

    string Errorlog = System.Configuration.ConfigurationManager.AppSettings["Errorlog"];

    string path = System.AppDomain.CurrentDomain.BaseDirectory.ToString();

    sBuilder.Append(DateTime.Now.ToString("dd/MM/yyyy") + " " + DateTime.Now.ToShortTimeString());
    sBuilder.Append(" | ");
    sBuilder.Append(Desc);
    sBuilder.Append(" | ");
    sBuilder.Append(ID);
    sBuilder.Append(" | ");
    sBuilder.Append(Name);
    sBuilder.Append(" | ");
    sBuilder.Append(ErrorMessage);

    StreamWriter sw = (!File.Exists(path)) ? File.CreateText(path) : File.AppendText(path);
    sw.WriteLine(sBuilder.ToString());
}

}

And here is how i call the writelog.

 Writelog.WritelogDesc("Desc", "ID", "Name", "ErrorMessage");

Upvotes: 7

Views: 28594

Answers (4)

Henrik Gering
Henrik Gering

Reputation: 1891

You could use filemon (http://technet.microsoft.com/en-us/sysinternals/bb896642.aspx) to inspect what is actually happening on the fil system.

This way you can see when your program tries to get handles to the file system and what files and directories your program wants.

Upvotes: 0

jheddings
jheddings

Reputation: 27573

It's not quite clear how you are trying to use the 'path' and 'Errorlog' variables. It looks like you are trying to create a file using the directory path.

You'll need to ensure that the base path for your log file exists before you create the file. Is this close to what you are looking for?

Directory.CreateDirectory(path);
String logfile = Path.Combine(path, Errorlog);
StreamWriter sw = new StreamWriter(logfile, true);

Upvotes: 8

Graviton
Graviton

Reputation: 83316

Your path is a directory (string path = System.AppDomain.CurrentDomain.BaseDirectory.ToString()), but you use it as a File (File.Exists(path))?

Maybe you should define path as something like this:

string path = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, Errorlog)

if Errorlog is your filename (e.g., "xys.txt")

Upvotes: 1

TheVillageIdiot
TheVillageIdiot

Reputation: 40512

Construct path to file:

string Errorlog=System.Configuration.ConfigurationManager.AppSettings["Errorlog"];
string path = System.AppDomain.CurrentDomain.BaseDirectory + Errorlog;
.......
.......
StreamWriter sw = (!File.Exists(path)) ? 
      File.CreateText(path) : File.AppendText(path);

sw.WriteLine(sBuilder.ToString());

Upvotes: 1

Related Questions