File not found using Task Scheduler

后端 未结 4 1333
甜味超标
甜味超标 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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: 
    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
    

提交回复
热议问题