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
CWindowsSystem32..
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]
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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:WindowsSystem32
So, your service gets the C:WindowsSystem32 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.
Method 2
It looks like access right issue. Unless granted you need administrative privileges to access C:WindowsSystem32 folder. On your local machine you might have access to path but on server you do not. Insted of setting C:WindowsSystem32 as startupPath in your code, try with Path.GetTempPath.
https://msdn.microsoft.com/en-us/library/system.io.path.gettemppath(v=vs.110).asp
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0