Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Design Time Path

I'm using a number of C# custom controls (forms, buttons, etc) which use a skinning system, and are dependent on external images (in a zip file) within the project folder. Right now, the form designer is unable to show the controls, because I can't get the correct path to the zip file. What I need is a way to get the path to the assembly or solution at design time.

I am using two projects:
DLL - Contains the custom controls.
Host application - References the DLL and uses the custom controls.

In my DLL custom control classes, at runtime, I'm simply using:

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

which works perfectly, but at design time, the form designer displays the error:

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

Having looked at similiar questions on the site, I've tried the following too:
1)

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

The form designer displays the 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 the error:

Object reference not set to an instance of an object.

3) A variety of different paths using the Assembly class.

Nothing as of yet has worked. I'm using Visual C# 2010 Express.

like image 810
Snip3r Avatar asked Nov 25 '11 21:11

Snip3r


1 Answers

Your 2nd attempt (ITypeResolutionService) should work fine. Just make sure you call GetService late enough, so Site property would be non-null. OnHandleCreated is fine, control constructor is way too soon and produces NullReferenceException.

like image 184
MagnatLU Avatar answered Oct 10 '22 19:10

MagnatLU