Reputation: 4727
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]
Upvotes: 1
Views: 3419
Reputation: 2266
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.
Upvotes: 1
Reputation: 11
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
Upvotes: 0