我正在开发一个Visual Studio扩展包。每次我测试运行时,VS的实验实例都会打开。我在实验实例中创建了一个项目;如何获得在实验实例中创建的项目的路径?
答案 0 :(得分:2)
首先应该导入EnvDTE(在EnvDTE.dll中)。然后我声明了属性ProejectFullName
string ProejectFullName{ get; }
表示项目对象文件的完整路径和名称的字符串。
public void GetmyFilePath(DTE2 dte)
{
try
{ // Open a project before running this sample.
Project prj = dte.Solution.Projects.Item(1);
Projects prjs;
string msg, msg2 = "Global Variables:";
msg = "FileName: " + prj.FileName;
msg += "\nProejectFullName: " + prj.ProejectFullName;
msg += "\nProject-level access to " + prj.CodeModel.CodeElements.Count.ToString() +
" CodeElements through the CodeModel";
prjs = prj.Collection;
msg += "\nThere are " + prjs.Count.ToString() + " projects in the same collection.";
msg += "\nApplication containing this project: " + prj.DTE.Name;
if (prj.Saved)
msg += "\nThis project hasn't been modified since the last save.";
else
msg += "\nThis project has been modified since the last save.";
msg += "\nProperties: ";
foreach (Property prop in prj.Properties)
{
msg += "\n " + prop.Name;
}
foreach (String s in (Array)prj.Globals.VariableNames)
{
msg2 += "\n " + s;
}
MessageBox.Show(msg, "Project Name: " + prj.Name);
MessageBox.Show(msg2);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
答案 1 :(得分:1)
给定代表项目层次结构的IVsHierarchy对象,在使用GetProperty调用VSHPROPID_ProjectDir / VSHPROPID_Name properties方法时,可以使用VSITEMID_ROOT ID。
或者,如果您有EnvDTE.Project,则可以使用EnvDTE.Project.FullName属性。
答案 2 :(得分:1)
您应声明属性ProjectName
,然后使用EnvDTE(在EnvDTE.dll中)使用该声明的属性。在您与EnvDTE相等ProjectName
后,您可以获得路径。