Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi,

I created windows service in same solution.

In one project I included the files in App_Data folders.

I want to get the files in service.

How can we get files in windows service from web project in same solution.

Please suggest me

What I have tried:

This is my service code

public void GetDashboards()
        {
            string sPath = HttpContext.Current.Server.MapPath(string.Format(@"~/App_Data/Dashboards"));
            string[] lstFiles = Directory.GetFiles(sPath, "*.xml");
//This folder is existing in different project in same solution.Here I got object reference not set to an instance error.

            foreach (string item in lstFiles)
            {
                FileInfo objFileInfo = new FileInfo(item);
                string sFileName = objFileInfo.Name.Replace(objFileInfo.Extension, "");
                UpdateExtract(sFileName);
            }
        }
Posted
Updated 7-Jun-17 0:44am
v2

1 solution

Your service can't access HttpContext because it is not running as the result of a web request, and it knows nothing about the web site's configuration such as where it is located on the disc. You're just going to have to tell the service the path by using a config file (app.config) and hard-coding the path into the config file and reading that config value using ConfigurationManager.
 
Share this answer
 
v2
Comments
TarunKumarSusarapu 7-Jun-17 5:31am    
Hi F-ES Sitecore,

Would you please modify the code

string sPath = HttpContext.Current.Server.MapPath(string.Format(@"~/App_Data/Dashboards"));
string[] lstFiles = Directory.GetFiles(sPath, "*.xml");
F-ES Sitecore 7-Jun-17 5:34am    
You can't modify it, what you are trying to do is not possible inside a windows service. Store the folder path in the app.config instead

https://stackoverflow.com/questions/2400097/reading-from-app-config-file
TarunKumarSusarapu 7-Jun-17 5:41am    
Suppost my project file path is C:\Temp\Original\SourceCode\AINIPMKPIDashboard\App_Data\Dashboards\Data Extracts\

How can add it in app.config file without hard coding.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900