stack stack - 3 months ago 12
ASP.NET (C#) Question

Could not find a part of path error on server

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

Answer

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.