Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

c# - XML file corrupted by other application

I have written two applications and one starts the other one using Process.Start(path_to_app2);

App2 starts as expected. Both of the applications save own xml configs files. and sometimes one xml ( that is first written by app 1 ) is overwritten by app 2 even if it is running in separate catalog.

this overwriting does not happen when app 2 is started alone from inside its own folder

when I run Save routine both of them have the same content in app1 and app2 I use the same conf.xml file name by assumption that both apps are in different forlders.

public void Save(string fileName)
        {
            string path = Environment.CurrentDirectory + "\" + fileName;
            //first serialize the object to memory stream,
            //in case of exception, the original file is not corrupted
            using (MemoryStream ms = new MemoryStream())
            {
                var writer = new System.IO.StreamWriter(ms);
                var serializer = new XmlSerializer(this.GetType());
                serializer.Serialize(writer, this);
                writer.Flush();

                //if the serialization succeed, rewrite the file.
                File.WriteAllBytes(path, ms.ToArray());
            }
        }


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...