S
Steve
I am trying to call a file delete function from inside Session_End in
Global.asax.cs. However, everytime I step into my delete function from
Session_End it jumps to the catch statement and I get the "Object
reference not set to an instance of an object." I don't understand why
it is happening. I would appreciate any help, thanks.
Global.asax.cs
using TheFile = Namespace.TheFile;
private TheFile sessionEnd = new TheFile();
protected void Session_End(Object sender, EventArgs e)
{
sessionEnd.DeleteFile();
}
TheFile.cs
public void DeleteFile()
{
try
{
string filePath = System.Web.HttpContext.Current.Server.MapPath(null);
FileInfo file = new FileInfo(filePath + "/FileName.txt");
if (!Directory.Exists(filePath))
{
// Try to create the directory.
DirectoryInfo di = Directory.CreateDirectory(filePath);
}
if (file.Exists)
file.Delete();
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.Message);
}
}
Global.asax.cs. However, everytime I step into my delete function from
Session_End it jumps to the catch statement and I get the "Object
reference not set to an instance of an object." I don't understand why
it is happening. I would appreciate any help, thanks.
Global.asax.cs
using TheFile = Namespace.TheFile;
private TheFile sessionEnd = new TheFile();
protected void Session_End(Object sender, EventArgs e)
{
sessionEnd.DeleteFile();
}
TheFile.cs
public void DeleteFile()
{
try
{
string filePath = System.Web.HttpContext.Current.Server.MapPath(null);
FileInfo file = new FileInfo(filePath + "/FileName.txt");
if (!Directory.Exists(filePath))
{
// Try to create the directory.
DirectoryInfo di = Directory.CreateDirectory(filePath);
}
if (file.Exists)
file.Delete();
}
catch (Exception e)
{
Console.WriteLine("Error: " + e.Message);
}
}