Repost: Getting Project Folder during Design Time

G

Guest

I was given the following code in response to a request to retrieving the
Web Root for a Project while at design time. However this does not do what I
need it to do. I am looking for the Current Project (which i assume is
objProject) and I am looking for its "Project Folder" ?

The Solution I was given is very buggy as it randomly gives different
project folders directories, or the last saved directory. for example i was
editng an html that was not part of the project about 7 hours ago. When I
went to Webform1.aspx it showed the path to the Html file i saved (even
though it was not a part of the project and had been edited and taken care
of along time ago)

object objCodeDomProvider =
((Hashtable)objServiceContainerServices)[domType];

EnvDTE.ProjectItem objProjectItem =
(EnvDTE.ProjectItem)objCodeDomProvider.GetType().InvokeMember

( "projectItem",

BindingFlags.Instance |BindingFlags.NonPublic| BindingFlags.GetField,

null,

objCodeDomProvider,

new object[] { }

);

// Create an EnvDTE.Project object

EnvDTE.Project objProject = objProjectItem.ContainingProject;

EnvDTE.Solution objSolution = objProject.DTE.Solution;

StringBuilder TheBuilder = new StringBuilder();

return objProject.DTE.ActiveDocument.Path;
 
S

Steven Cheng[MSFT]

Hi Johndoe,

From your description, you're developing asp.net web control and use the
DnvDTE namespace's interfaces
===================
objProject.DTE.ActiveDocument.Path;
===================
to get the current active project's(or items in it)'s path. But find the
result is not what you expected, yes?

As for this problem, it is because the ActiveDocument or the Active Project
get from the DNVDTE's inferfaces is not actually the active one if there're
more than one VS.NET ide opened. It will return the first opened VS.NET
instance. So this means works only if we open only one vs.net instance when
developing.
You can also see another approach in the following link:
http://blogs.msdn.com/mszcool/archive/2004/06/30/169793.aspx

And though the DnvDTE's fix behavior is like the above I mentioend, we can
use some COM interop means to workaround it so as to get the actual active
project in VS.NET ide, here is a demo I've made and also tested on myside,
please have a look to see whether it help. Thanks.

=======================================
namespace MyWebControlLib
{
public class TestControlDesigner : System.Web.UI.Design.ControlDesigner
{

public override string GetDesignTimeHtml()
{
string html = "";

try
{
string strMoniker = "!VisualStudio.DTE.7.1:" +
System.Diagnostics.Process.GetCurrentProcess().Id.ToString();

EnvDTE.DTE dte =(EnvDTE.DTE)GetMSDEVFromGIT(strMoniker);
html = "<br>" + dte.ActiveDocument.FullName;


}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}

return html;
}


[DllImport("ole32.dll")]

public static extern int GetRunningObjectTable(int reserved, out

UCOMIRunningObjectTable prot);



[DllImport("ole32.dll")]

public static extern int CreateBindCtx(int reserved, out UCOMIBindCtx
ppbc);





public object GetMSDEVFromGIT(string strProgID)

{

UCOMIRunningObjectTable prot;

UCOMIEnumMoniker pMonkEnum;



GetRunningObjectTable(0,out prot);

prot.EnumRunning(out pMonkEnum);

pMonkEnum.Reset();

int fetched;

UCOMIMoniker []pmon = new UCOMIMoniker[1];

while(pMonkEnum.Next(1, pmon, out fetched) == 0)

{

UCOMIBindCtx pCtx;

CreateBindCtx(0, out pCtx);

string str;

pmon[0].GetDisplayName(pCtx,null,out str);

if(str == strProgID)

{

object objReturnObject;

prot.GetObject(pmon[0],out objReturnObject);

object ide = (object)objReturnObject;

return ide;

}

}

return null;

}



}



[DefaultProperty("Text"),
ToolboxData("<{0}:TestControl runat=server></{0}:TestControl>"),
Designer(typeof(TestControlDesigner))]
public class TestControl : System.Web.UI.WebControls.WebControl
{
private string text;

[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public string Text
{
get
{
return text;
}

set
{
text = value;
}
}

protected override void Render(HtmlTextWriter output)
{
output.Write("<center><font size='20'>Hello World!</font></center>");
}
}
}
================================================

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top