hi to all i am trying to get a access a folder in my thread like this
protected string GetFolderName(int OrgID)
{
string FolderName = string.Empty;
string[] strDirectories = Directory.GetDirectories(HttpContext.Current.Server.MapPath("~/Uploads/"));
if (strDirectories.Length > 0)
{
for (int count = 0; count < strDirectories.Length; count++)
{
string name = strDirectories[count].Substring(strDirectories[count].LastIndexOf("\\") + 1);
if (name.Contains("_"))
{
string companyId = name.Substring(name.LastIndexOf("_") + 1);
if (Convert.ToInt32(companyId) == OrgID)
{
FolderName = name;
break;
}
}
}
}
return FolderName;
}
This method is invoked through the thread pool and it’s giving me an error of “Object reference not set to an instance of an object” on this line
string[] strDirectories = Directory.GetDirectories(HttpContext.Current.Server.MapPath("~/Uploads/"));
Please help me out
Solution
i used HttpRuntime.AppDomainAppPath in place of HttpContext.Current.Server.MapPath and it works perfectly.
HttpContext.Currentreturns the current context in the executing thread. The thread pool thread isn’t responsible for any HTTP request, so it doesn’t have a context. TheCurrentproperty will return null, which is why you’re getting the exception. I suggest you callDirectory.GetDirectories()before you transfer to the thread pool. An alternative would be to pass the context instead.EDIT: If you don’t want to perform
Directory.GetDirectories()in the thread pool thread, you could at least evaluateHttpContext.Current.Server.MapPath("~/Uploads/")in the original thread, and make that available to the thread pool. Basically you just want to avoid evaluatingHttpContext.Currenton the wrong thread.