File not found using Task Scheduler

后端 未结 4 1334
甜味超标
甜味超标 2020-12-30 10:59

In my C# code, I reference to an XML file \"file.xml\", which is in the same directory as the executable itself, using XmlDocument.

相关标签:
4条回答
  • 2020-12-30 11:15

    Try to set startup directory for the task. You can set in Task Scheduler.

    Select Task -> Right Click -> Properties -> Actions Tab -> Select Action -> Edit -> Start in (optional)

    0 讨论(0)
  • 2020-12-30 11:17

    You need to set the application's current working directory to the correct path. I don't know what it is when Task Scheduler launches your application, but it's never a good idea to assume.

    You can do so with Directory.SetCurrentDirectory().

    Alternatively, you can use an absolute path to the XML file.

    0 讨论(0)
  • 2020-12-30 11:26

    Try refering to your document by using

    Application.StartupPath(@"\file.xml")
    
    0 讨论(0)
  • 2020-12-30 11:35

    I met the same problem, and i tried to print out the various path that .Net sets for the program, here's the result on my machine

    AppDomain.CurrentDomain.BaseDirectory: <my program folder>
    Directory.GetCurrentDirectory();: C:\Windows\system32
    Environment.CurrentDirectory: C:\Windows\system32
    

    so you probably want to use AppDomain.CurrentDomain.BaseDirectory to locate your file, like this:

    string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "file.xml");
    // use this path to open xml and do your work with it
    
    0 讨论(0)
提交回复
热议问题