如何从同一解决方案中的项目文件夹中获取文件到服务 [英] How to get files from project folders in same solution to service

查看:80
本文介绍了如何从同一解决方案中的项目文件夹中获取文件到服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在相同的解决方案中创建了Windows服务。



在一个项目中我包括App_Data文件夹中的文件。



我想要提供服务中的文件。



我们怎么办?在同一解决方案中从web项目获取Windows服务中的文件。



请建议我



我尝试过:



这是我的服务代码



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);
            }
        }

推荐答案

您的服务无法访问HttpContext,因为它未运行因为一个Web请求,它对网站的配置一无所知,例如它在光盘上的位置。您只需要通过使用配置文件(app.config)告诉服务路径,并将路径硬编码到配置文件中并使用ConfigurationManager读取该配置值。
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.


这篇关于如何从同一解决方案中的项目文件夹中获取文件到服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆