How to get the project path during development

I am using the component (System.ComponentModel.Component), and I want to get the application path to my project in order to create a file in it.

thanks

Florian

+4
source share
9 answers

The only thing that seemed to work (sequentially) for me was to get EnvDTE.DTE (from IServiceProvider, which you get from EditValue ()), i.e.:

EnvDTE.DTE dte = envProvider.GetService(typeof(EnvDTE.DTE)) as EnvDTE.DTE; string dir = Path.GetDirectoryName(dte.Solution.FullName); 

When I tried to use Assembly.GetXAssembly, I got a temporary path that VS uses during development.

+3
source

Use AppDomain.CurrentDomain.BaseDirectory.

0
source

It works?

  New Uri(Assembly.GetCallingAssembly().CodeBase).AbsolutePath 

("CallingAssembly" because you can put a method to get the execution path to the service level (assembly))

0
source

Take a look: The path to the Project (bin) folder at compile time?

With this technique, you can β€œCreate” (once) and then use the generated file to get the project location in DesignTime. This will work regardless of the workspace.

0
source

I think Robert is almost right.

It works:

 Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) 
0
source
 string solutionpath = Directory.GetParent(Application.ExecutablePath).Parent.Parent.Parent.FullName; 

I think this is the best solution because you do not need to add any library more than "Using System.IO" :)

0
source

I did a simple test (as a temporary project, I added a string property to store the current directory value) (I do not describe the whole process of data binding. See part of my development time / binding time message)

In the empty constructor of the main window of the ViewModel class (dedicated to design time binding)

 public MainWindow_ViewModel():this(null) { dtpath = Directory.GetCurrentDirectory(); Console.WriteLine("CTor finished"); } 

In the mainwindow.xaml file, I added a text box to display the result (do not pay attention to the value of the grid line)

 <TextBox Text="{Binding dtpath}" Grid.Row="3"/> 

And I got an idea about VS design (as Florian commented, but with a newer value after 4 years): C: \ Program Files (x86) \ Microsoft Visual Studio 14.0 \ Common7 \ IDE

enter image description here

0
source

Just call GetMyPath, which is defined as

 string GetMyPath([CallerFilePath] string from = null) { return from; } 
0
source

If you're talking about a WPF designer, 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

Source: https://habr.com/ru/post/1303818/


All Articles