How to get application directory from my WPF application during development?

How to get the application directory from my WPF application during development? I need to access the resource in the current application directory during development, while my XAML is displayed in the designer, I cannot use the solution indicated in this question , since both System.IO.Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) and System.Reflection.Assembly.GetExecutingAssembly().Location indicate the location of the IDE (Visual Studio ... Common7 or something else).

Upon request, to further clarify my goals: I want to access the database table during development and display a graph of this data. The design was done in Visual Studio 2008, so I need a very specific solution for a very specific problem, and this is getting the build directory for my application.

+4
source share
5 answers

From your description, it looks like your code actually works inside WPF Designer in Visual Studio, for example, it is part of the custom management library that is used for development.

In this case, Assembly.GetEntryAssembly() returns null, but the following code gets the path to the application directory:

  string applicationDirectory = ( from assembly in AppDomain.CurrentDomain.GetAssemblies() where assembly.CodeBase.EndsWith(".exe") select System.IO.Path.GetDirectoryName(assembly.CodeBase.Replace("file:///", "")) ).FirstOrDefault(); 

The following steps can be used to demonstrate these work in the WSF Designer VS.NET 2008 tool:

  • Put this code inside the "WPF User Control Libraries" or "Class Library"
  • Add the code needed to read the database and return the data to display (in my case, I just returned the application directory as a string)
  • Link to the project of the project library that you are designing.
  • Use user controls or classes from the XAML file to populate your DataContext or otherwise provide data to your user interface (in my case, I linked the DataContext using x: Static)
  • Edit this XAML file using the "Windows Presentation Foundation Designer", which can be done by simply double-clicking if you have not changed your default editor, in which case use "Open With ..."

When you complete the following steps, the object you are looking at will be populated with data from your database equally at run time and development time.

There are other scenarios in which the same technique works just as well, and there are other solutions available depending on your needs. Please let us know if your needs are different from those I anticipated above. For example, if you are writing a VS.NET add-in, you are playing a completely different game.

+5
source

I do not think this is possible - you are asking for the location of an assembly that has not even been built yet. Your development-time code does not run inside your application and will have to make some assumptions about the IDE. It seems wrong and fragile for me - consider the following questions:

  • Is a project created?
  • If not, there is no executable to get the path, so then?
  • Will other files be present if they were not built or they do not create artifacts?
  • If it was built, where was it created?
  • Need to consider other IDEs?

In this situation, you should probably ask the user during development to provide or view the path by adding a property to your object for editing. Then your development-time code can use the value of the property to find what it needs.

0
source

Are you trying to support a designer (such as a visual studio designer or Blend)?

If so, there are various ways to solve this problem. Usually you do not want to rely on the relative path from the executable file, since it can be placed in various design tools (VS, Expression Blend, etc.).

Perhaps you can explain in more detail the problem you are trying to solve so that we can give a better answer?

0
source

If you are working intensively on WPF designers using adorner, etc., use the context type property / type

Details: - During development, you have an instance of modelItem (suppose you know this), if not, then you can create an instance of it in the Override implementation of the Activate method

// in class DesignAdorner

 public class DesignAdorner : PrimarySelectionAdornerProvider { protected override void Activate(ModelItem item) { modelItem = item; } } 

Now you can access the current application path using the following code with one line

 string aplicationPathDir = System.IO.Directory.GetParent(modelItem.Context.ToString()).FullName; 

Let me know if this does not help you.

0
source

Well, giving further clarification, that is what I will do.

Remaining in line with the care taken by GraemeF, doing what you want is fragile and prone to cracking at best.

In this regard, the general practice is to consider support for development-time data as a completely different approach, and then support for run-time data. Very simply, the connection you create between your temporary development environment and this database is a bad idea.

To just provide design-time data for visualization, I prefer to use the mock class, which adheres to the common interface as the runtime class. This gives me a way to show the data that I can provide of the correct type and conforms to the same contract as my runtime object. However, this is a completely different class that is used to support development time (and is often used for Unit Testing).

For example. If I had a runtime class that should show the person’s data, such as first name, last name and email address:

 public class Person() { public String FirstName { get; set;} public String LastName {get; set;} public Email EmailAddress {get; set;} } 

and I populated this object from the database at runtime, but should also provide a visualization of the development time. I would introduce the IPerson interface, which defines the contract to which it is bound, namely, provides that getters properties exist:

 public interface IPerson() { String FirstName { get; } String LastName { get; } Email EmailAddress { get; } } 

Then I would update my runtime Person class to implement the interface:

 public class Person() : IPerson { public String FirstName { get; set;} public String LastName {get; set;} public Email EmailAddress {get; set;} } 

Then I would create a mock class that implements the same interface and provides reasonable values ​​for using development time

 public MockPerson() : IPerson { public String FirstName { get { return "John"; } } public String LastName { get { return "Smith"; } } public Email EmailAddress { get { return new Email(" John@smith.com "); } } } 

Then I would implement a mechanism for providing a MockPerson object at design time and a real Person object at run time. Something like this or this . This provides support for development-time data without a tight relationship between the runtime and development time.

This template is much more flexible and allows you to provide ongoing support for development-time data in your application.

-2
source

All Articles