0

我想在我的解决方案中创建动态项目

C# 代码

protected void Page_Load(object sender, EventArgs e)
        {
            string projectPath = @"C:\Users\lucas_000\Documents\Visual Studio 2010\Projects\ModelingProject1\ModelingProject1\ModelingProject1.modelproj";
            using (IModelingProjectReader projectReader = ModelingProject.LoadReadOnly(projectPath))
            {
               IModelStore store = projectReader.Store;
               foreach (IClass umlClass in store.AllInstances<IClass>())
               {
                   try
                   {
                       Engine engine = new Engine();                       
                       //engine.BinPath = RuntimeEnvironment.GetRuntimeDirectory();
                       Project project = engine.CreateNewProject();

                       project.AddNewImport(@"$(MSBuildBinPath)\Microsoft.CSharp.targets", null);

                       BuildPropertyGroup props = project.AddNewPropertyGroup(false);
                       props.AddNewProperty("AssemblyName", "myassembly");
                       props.AddNewProperty("OutputType", "Library");

                       BuildItemGroup items = project.AddNewItemGroup();
                       items.AddNewItem("Reference", "Some.Assembly");
                       items.AddNewItem("Compile", "somefile.cs");

                       project.Save("myproject.csproj");
                   }
                   catch (Exception e2)
                   {
                   }
               }
            }
        }

问题

何时保存项目,发生UnauthorizedAccessException在我不想将这个新项目保存在那里的文件夹中。

如何在 Visual Studio 2010 的当前解决方案中创建此项目?

更新

线路project.Save("myproject.csproj")试图保存在C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\myproject.csproj

我所有的解决方案都在C:\Users\lucas_000\Documents\Visual Studio 2010\Projects

4

0 回答 0