Adding files to a project by code RRS feed

  • Question

  • Hi

    I am writing a lot of BLL DAL classes for data so decided to make a code generator for speed. That works great and has reduced my coding time exponentially over the past week. I decided to make this as an addin to create the .vb files, place them into my project folder then add them to the project programatically. I have been using this Class  but im having some issues with it.

        Friend Sub AddToProject(ByVal ProjName As String, ByVal FilePath As String, ByVal FileName As String, ByVal SubFolder As String)
            Dim p = GlobalProjectCollection.LoadedProjects.FirstOrDefault(Function(pr) pr.FullPath = ProjName)
            If p Is Nothing Then p = New Microsoft.Build.Evaluation.Project(ProjName)
            Dim folderLoc = System.IO.Path.Combine(SubFolder)
            If p.Items.FirstOrDefault(Function(i) i.EvaluatedInclude = folderLoc) Is Nothing Then p.AddItem("Folder", folderLoc)
            Dim fileLoc = System.IO.Path.Combine(SubFolder, FileName & ".vb")
            If p.Items.FirstOrDefault(Function(i) i.EvaluatedInclude = fileLoc) Is Nothing Then p.AddItem("Compile", fileLoc)
        End Sub

    I browse to the project using a file dialog and select vbproj file which is then passed in the call as 'GetFilePath'. I then call it like this

    Dim BLLPath As String = System.IO.Path.Combine(GetFolderPath, "Data", "BLL", GetClassName & ".vb")
    Dim DALPath As String = System.IO.Path.Combine(GetFolderPath, "Data", "DAL", GetClassName & ".vb")
    'check if folders exist, create if they dont
    FileOrDirExists(System.IO.Path.Combine(GetFolderPath, "Data", "BLL"))
    FileOrDirExists(System.IO.Path.Combine(GetFolderPath, "Data", "DAL"))
    'save BLL to the folder path
    Dim BLLText As String = Xucbll1.TxtOutput.Text
    My.Computer.FileSystem.WriteAllText(BLLPath, BLLText, False)
    'save DAL to the folder path
    Dim DALText As String = Xucbll1.TxtDALOutput.Text
    My.Computer.FileSystem.WriteAllText(DALPath, DALText, False)
    Dim AP As New AddToProject
    AP.AddToProject(GetProject, GetFolderPath, GetClassName, "Data\BLL")
    AP.AddToProject(GetProject, GetFolderPath, GetClassName, "Data\DAL")

    and the 2 files are added to the project folder Data/BLL & Data/DAL folders.

    The files are added to the project and then im prompted for a project reload. The problem is once that process is complete. My project settings get an error

    So, something is operating properly. Can anyone identify where i am going wrong?



    I am here to ask questions and learn from others. Cor Ligthert DO NOT REPLY TO ANY OF MY POSTS!!

    Monday, November 23, 2020 4:40 PM

All replies

  • forum migrated to mentioned below link please use this


     hope answer the question if issue resolve please accept answer
    Monday, November 23, 2020 4:55 PM
  • forum migrated to mentioned below link please use this


     hope answer the question if issue resolve please accept answer
    Please stop posting a migration alert without recommending a solution as doing so will only annoy regulars here in this and notably the C# forum. There are announcements already for both forums.  

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    My GitHub code samples
    GitHub page

    Monday, November 23, 2020 6:44 PM
  • Hi NachoShaw,

    Thank you for posting here.

    In order to help us reproduce your problem, could you provide more details about your 'GetProject', 'GetFolderPath' and 'GetClassName' method? It will help us make a test.

    We are waiting for your update.

    Best Regards,

    Xingyu Zhao

    Visual Basic and CLR forum will be migrating to a new home on Microsoft Q&A! (VB.NET and CLR) We invite you to post new questions in the new home on Microsoft Q&A ! For more information, please refer to the sticky post(VB.NET and CLR).

    Tuesday, November 24, 2020 6:45 AM