反序列化不适用于任务计划程序

时间:2013-11-07 13:19:40

标签: c# xml-serialization scheduled-tasks

我有一个程序可以从XML文件中反序列化一些数据。当我使用VS Debugger启动程序或直接单击.exe文件时,这非常正常。但是,当我使用Microsoft任务计划程序启动程序时,反序列化不起作用,但程序启动。该任务直接指向包含xml文件和.exe。

的目录

方法:

    internal static Settings DeserializeSettings()
    {
        var path = Directory.GetCurrentDirectory() + "\\settings.xml";
        XmlSerializer mySerializer = new XmlSerializer(typeof(Settings));

        using (FileStream myFileStream =
        new FileStream(path, FileMode.Open))
        {
            Settings test = new Settings();
            if (!(myFileStream.Length <= 0))
            {
                 test = (Settings)mySerializer.Deserialize(myFileStream);
            }
            return test;
        }
    } 

1 个答案:

答案 0 :(得分:3)

很可能它没有在正确的目录中执行。当您创建计划任务时,您需要告诉它从哪个目录开始。否则它将从默认目录开始,这几乎肯定不是您想要的目录。

通常,您将应用程序设置放在应用程序的App.Config文件中。如果你使用内置设置的东西,它都是为你处理的。参见:

相关问题