Could not find part of path error on server

I want to run the scheduler on a daily basis. Therefore, I created a Windows application and saved it on the server.

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

Could not find part of the path

C \ Windows \ System32 ..

With this, I think there may be some kind of problem associated with this path.

Here is my code for this.

 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 happened with this path,

Here is a screenshot of the error

Mistake

+1
c # path server
02 Sep '16 at 11:00
source share
2 answers

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

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

You can try:

startupPath = System.AppDomain.CurrentDomain.BaseDirectory;

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

+1
Sep 02 '16 at 11:20
source share

It looks like a permission issue. If you are not granted, you need administrator rights to access the C: \ Windows \ System32 folder. On your local computer, you may have access to the path, but you do not have access to the server. If you installed C: \ Windows \ System32 as startupPath in your code, try using Path.GetTempPath.

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

0
Sep 02 '16 at 11:08 on
source share



All Articles