1

I want to run a scheduler on daily basis. So I have created a Windows application and stored it onto the server.

This works fine on my local machine, but I get path error as

Could not find a part of path

C\Windows\System32..

With this, I think there might be some issue related to the path.

Here is my code for that.

startupPath = Environment.CurrentDirectory;
                    strExp = "RAName = '" + group.Key + "'";

                    DataTable dtNew = ds.Tables[1].Select(strExp).CopyToDataTable();
                    DataSet dsNew = new DataSet();
                    dsNew.Tables.Add(dtNew);

                    dtNew.Columns.Remove("RAName");
                    dtNew.Columns.Remove("UserEmail");
                    ExcelLibrary.DataSetHelper.CreateWorkbook(startupPath + "\\Attachment\\Reminder_Sheet_ " + dtNew.Rows[0]["SR NO"].ToString() + ".xls", dsNew);

                    ls_attach1.Add(startupPath + "\\Attachment\\Reminder_Sheet_ " + dtNew.Rows[0]["SR NO"].ToString() + ".xls");

                    foreach (var attach in ls_attach1)
                    {
                        mail.Attachments.Add(new Attachment(attach));
                    }

                    ce.SendEmail(tb_RA.Rows[0]["RA1_Email"].ToString(), "", "", "Information on documents for processing", sbodyMail,
                                                        "AUTOSQL", "Powersoft", ls_attach1, "ConnectionString");
                    foreach (Attachment attachments in mail.Attachments)
                    {
                        attachments.Dispose();
                    }

                    if ((System.IO.File.Exists(startupPath + "\\Attachment\\Reminder_Sheet_ " + dtNew.Rows[0]["SR NO"].ToString() + ".xls")))
                    {
                        System.IO.File.Delete(startupPath + "\\Attachment\\Reminder_Sheet_ " + dtNew.Rows[0]["SR NO"].ToString() + ".xls");
                    }
 

I don't know what's wrong with the path here,

Here is the screenshot of the error

[![Error][1]][1]

Community
  • 1
  • 1
Nad
  • 4,605
  • 11
  • 71
  • 160
  • how do you initialize `startupPath` variable? It might not be getting initialized in proper way. – Sagar Sep 02 '16 at 11:05
  • What is `startupPath` in your case? – Divyang Desai Sep 02 '16 at 11:05
  • I take it that you already have the folder `C:\Windows\System32\Attachment` created, before trying to create the excel file in it. You might need to make sure that the user that runs your service has priviliges to write into the folder. Setting _Log On As_ value of the service to Local System could deal with the issue. – uTeisT Sep 02 '16 at 11:06
  • @sagar: Updated the question – Nad Sep 02 '16 at 11:09
  • @Div: updated the question, have a look – Nad Sep 02 '16 at 11:10
  • 1
    @uteist assuming that `C:\Windows\System32` would be a base path, your solution will work. But it is highly unlikely that someone will give such a base path. Chances of wrong initialization of base path seems to be high. – Sagar Sep 02 '16 at 11:10
  • @Div: Nope, In windows we cant use that. – Nad Sep 02 '16 at 11:18
  • @stack For debugging purpose I would suggest you to try to read your base path value from app.config file. If your base path is not going to change then read it from app.config or if possible try to read it from database. – Sagar Sep 02 '16 at 11:18
  • @sagar: How can I read from `app.config`? can u show me.Please – Nad Sep 02 '16 at 11:19
  • @stack You will have to use `ConfigurationManager.AppSettings[key]`. Hit this link [msdn](https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings(v=vs.110).aspx) for detailed usage. – Sagar Sep 02 '16 at 11:22
  • @stack@uteist has given a more relevant answer. Try out it. It should work. – Sagar Sep 02 '16 at 11:25
  • Why did you tag this as ASP.NET? – mason Sep 02 '16 at 12:37

2 Answers2

1

You probably assumed that when you installed your service, it'd run on the path where it is installed from but services on Windows are run by "Service Control Manager" (scm) which is usually located on C:\Windows\System32

So, your service gets the C:\Windows\System32 value as CurrentPath()

You could try:

startupPath = System.AppDomain.CurrentDomain.BaseDirectory;

*Edit: For those who might want to check the path for scm, the file that you need to check is sc.exe As in sc command that you use to install,start,etc. a service.

uTeisT
  • 2,256
  • 14
  • 26
0

It looks like access right issue. Unless granted you need administrative privileges to access C:\Windows\System32 folder. On your local machine you might have access to path but on server you do not. Insted of setting C:\Windows\System32 as startupPath in your code, try with Path.GetTempPath.

https://msdn.microsoft.com/en-us/library/system.io.path.gettemppath(v=vs.110).asp

Jaydeep
  • 11
  • 3