0

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());
            }
        }

Seba Ja
  • 41
  • 1
  • 5
  • There's an override to Process.Start, which allows you to provide startup information, including a working directory. I believe the default is to use that of the calling app. – 500 - Internal Server Error Jan 06 '21 at 18:09
  • 2
    Environment.CurrentDirectory is the current working directory. Not necessarily the .exe's location. Use System.Reflection.Assembly.GetExecutingAssembly().Location to get the .exe's location. – Jay Buckman Jan 06 '21 at 18:09
  • Thank You very much I will check it tomorrow morning. Regards – Seba Ja Jan 06 '21 at 21:40
  • Yes that was the reason. When I used suggested solution or AppDomain.CurrentDomain.BaseDirectory thenthe problem stopped. Thank You for answers. – Seba Ja Mar 16 '21 at 08:35

0 Answers0