locked
Obtaining a relative path during design time RRS feed

  • Question

  • I have a directory named "Configuration" at the root level of a WPF project.  I use that for files that will allow the end user to add xml configuration data that will alter the application's behavior. (Yes... there's a Blend question coming...)

    When running live, the code in my configuration window's veiwmodel gets the current directory (via Directory.GetCurrentDirectory), appends @"/Configuration/" and the filename to it, and all my files are read just fine.  Note for later use that the file fetching is done in the constructor of the viewmodel, which is initiaited due to being the datacontext of the configuration window.)

    Ok.. the problem:  When I view the configuration window in Blend I get an error indicatiing that there was a problem during the bind to the datacontext.  It's because the path from GetCurrentDirectory is incorrect.  I made a string property out of it so I could see it in Blend, and it's pointing to an empty directory in C:\users\myName\AppData\Local\Temp\w4szdhg0.nho.  If I then try to get the path with Assembly.GetExecutingAssembly().CodeBase, I get C:\users\myName\AppData\Local\Microsoft\Expression\Blend\Project Assemblies\pxwmhwgp.  This directory contains two files only:  myapp.exe and it's pdb file.

    In the configuration window of this application, I have a design-time desire to be able to use some of the configuration data when building new UI.

    So... how can I determine where the "real" project is from code running in my viewmodel during design time so that my viewmodel can read a file in my configuration directory, (which will in turn show these results to the designer.)

    Thanks,
    Jim
    Tuesday, March 16, 2010 1:18 AM

Answers

  • Hi Jim,

    How about using

    System.Environment.CurrentDirectory

    Does that work? What you are seeing is expected - we do copy the assemblies into a "shadow" folder when we load them into the design surface.

    Thanks,
    Unni


    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Marked as answer by Jim Tomasko Wednesday, March 17, 2010 10:55 PM
    Wednesday, March 17, 2010 6:17 PM

All replies

  • Hi Jim,

    How about using

    System.Environment.CurrentDirectory

    Does that work? What you are seeing is expected - we do copy the assemblies into a "shadow" folder when we load them into the design surface.

    Thanks,
    Unni


    This posting is provided "AS IS" with no warranties, and confers no rights.
    • Marked as answer by Jim Tomasko Wednesday, March 17, 2010 10:55 PM
    Wednesday, March 17, 2010 6:17 PM
  • Thanks Unni... System.Environment.CurrentDirectory works perfect.

    It seems at times that the .NET assemblies solve just about every problem we will have, if only we could find the classes we need.

    I would have never guessed that Directory.GetCurrentDirectory would return an answer different from System.Environment.CurrentDirectory, but as they say, "live and learn".

    Thanks again
    Wednesday, March 17, 2010 10:59 PM