Could not find a part of path error on server

跟風遠走 提交于 2019-12-02 11:45:31

问题


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


回答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.




回答2:


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



来源:https://stackoverflow.com/questions/39290793/could-not-find-a-part-of-path-error-on-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!