Search code examples
c#wpfrelative-pathabsolute-path

Confusion over relative and absolute paths


I have a WPF program which deals with images on a canvas.

I am at the stage where I am trying to use serialization to be able to save the contents of my program and reload it at a later stage.

So At the moment when I am inserting any images into a control I am using absolute path values, I understand that this would be a bad idea for a program where I am wanting to save the state of the program and reload it at a later time.

So what is the best course of action to take in this situation.

Do I create a folder inside my WPF project for example called Images and then do I copy all Images I use in my program to this folder and then point the path to this?

Or am I completely on the wrong lines here?


Solution

  • If you are serializing the state data of your application, you would usually create a folder in one or more of the so-called system special folders, which you can get by a call to Environment.GetFolderPath.

    You may for example store data with application scope (same for all users) in a folder below the special folder specified by the SpecialFolder.CommonApplicationData enum (which is C:\ProgramData on Windows 7 systems).

    Data that is specific for the current roaming user (who works on multiple computers on a network), would be stored in a folder below SpecialFolder.ApplicationData. There is also SpecialFolder.LocalApplicationData for the non-roaming user.

    You may take a look at the Environment.SpecialFolder enumeration to get an overview.