문제

I have created a console application, i have tested and verified the functionality on local machine it is working fine. When i create scheduler using this application on Production(live) Server. I have pasted the release folder in "intpub/wwwroot/Utility/". now when i run the code it throws me error, i.e code is not able to find file to which i need to read in that process. it is searching the file on "Windows/System32" folder. I don't know why. as there are some other schedulers also working from the same "Utility" folder. reading files from there.

Here is my code:

static void Main(string[] args)
    {
        try
        {
            DateTime DateFrom = DateTime.Now;
            DateTime DateTo = DateFrom.AddDays(-1);
            string BrochureRequest = string.Empty;
            string RequestEmail = string.Empty;
            string RequestLocation = string.Empty;

            StreamReader EmailList = new StreamReader("brochure_request_list_New.txt");
            while (EmailList.Peek() >= 0)
            {
                BrochureRequest = EmailList.ReadLine();
                if (BrochureRequest.IndexOf(",") > -1)
                {
                    RequestEmail = BrochureRequest.Substring(0, BrochureRequest.IndexOf(","));
                    RequestLocation = BrochureRequest.Substring(BrochureRequest.IndexOf(",") + 1);

                    SendBrochureRequests(RequestEmail, "", DateTo, DateFrom, RequestLocation);
                }
            }
            EmailList.Close();
        }
        catch (Exception AppError)
        {
            MailMessage ErrorMail = new MailMessage();
            ErrorMail.From = new MailAddress(ConfigurationManager.AppSettings["FromEmailAddress"]);
            ErrorMail.To.Add(ConfigurationManager.AppSettings["ErrorNotifyEmailAddress"]);
            ErrorMail.Subject = "Automailer Error";
            ErrorMail.Body = AppError.ToString();
            SmtpClient SmtpMail = new SmtpClient();
            SmtpMail.Send(ErrorMail);
        }
    }

"brochure_request_list_New.txt" is the file about which i am talking. i don't know why this happens, please help me to rectify. I want to use this file from "Utility" folder.

도움이 되었습니까?

해결책

I think you should better explain your scenario, but looks like you have a different "Current Directory" in the server.

When you open your StreamReader you have a relative path, which is perhaps being interpeted as being relative to the Environment.CurrentDirectory.

I'd suggest 2 solutions:

  • Get your executable directory and combine that with the file name to get a full path (see here)

  • Assuming that by scheduler you mean the Windows Task Scheduler, you can set the start directory in your action definition:

see here

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top