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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With