C # Design Time Path

I use several C # user controls (forms, buttons, etc.) that use the skins system and depend on external images (in the zip file) in the project folder. At the moment, the form designer cannot display the controls because I cannot get the correct path to the zip file. I need a way to get the path to the assembly or solution during development.

I use two projects:
DLL - contains user controls.
Host application - DLL links and the use of user controls.

In my runtime user control DLL classes, I simply use:

string skinPath = "./Skins/" + skin + ".zip"; 

which works fine, but during development, the form constructor displays an error:

 Could not find a part of the path 'C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\Skins\Black.zip'. 

Looking at similar questions on the site, I also tried the following:
1)

 if (designMode) { EnvDTE.DTE dte = GetService(typeof(EnvDTE.DTE)) as EnvDTE.DTE; string path = Path.GetDirectoryName(dte.Solution.FullName); } 

The form designer displays an error:

 Object reference not set to an instance of an object. 

2)

 if (designMode) { ITypeResolutionService typeResService = GetService(typeof(ITypeResolutionService)) as ITypeResolutionService; string path = typeResService.GetPathOfAssembly(Assembly.GetExecutingAssembly().GetName()); } 

The form designer displays an error:

 Object reference not set to an instance of an object. 

3) Many different paths using the Assembly class.

Nothing has worked yet. I am using Visual C # 2010 Express.

+6
source share
1 answer

Your second attempt ( ITypeResolutionService ) should work fine. Just make sure you call GetService late enough, so the Site property will not be null. OnHandleCreated fine, the control constructor is too soon and NullReferenceException .

+1
source

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


All Articles