如何使用反射将输出保存到文件夹

时间:2013-06-27 18:34:53

标签: c# reflection xmlserializer

我有一个用于创建示例xml数据的测试。目前输出正保存到我的桌面。我想要做的是将文件保存到测试项目中的文件夹,即/outputs。有没有办法可以用反射或其他方法做到这一点?

[TestMethod]
        public void ProduceSampleIndicativeData()
        {
            var process_serializer = new XmlSerializer(typeof(ProcessIndicativeDataType));
            var sync_serailizer = new XmlSerializer(typeof (SyncIndicativeDataType));

            XmlWriter process_writer = new XmlTextWriter(@"C:\Users\...\Desktop\ProcessIndicativeData.xml", new UTF8Encoding());
            XmlWriter synch_writer = new XmlTextWriter(@"C:\Users\...\Desktop\SyncIndicativeData.xml",new UTF8Encoding());

            var namespaces = new XmlSerializerNamespaces();
            namespaces.Add("", "http://www.hr-xml.org/3");
            namespaces.Add("oa", "http://www.openapplications.org/oagis/9");
            namespaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");

            process_serializer.Serialize(process_writer, processIndicativeData, namespaces);
            sync_serailizer.Serialize(synch_writer,syncIndicativeData,namespaces);

        }

我试过

string ProcessIndicativeDataLocation = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"OutputFiles\ProcessIndicativeData.xml");

但这指向C:\Users\...\AppData\Local\Temp\...

1 个答案:

答案 0 :(得分:1)

System.IO.Directory.GetCurrentDirectory()怎么样 http://msdn.microsoft.com/en-us/library/system.io.directory.getcurrentdirectory.aspx