مشاركة عبر


كيفية القيام بما يلي: التعامل مع Visual Basic و C# مشاريع باستخدام كائن VSProject2

VSProject2يوفر واجهة الوصول إلى أساليب وخصائص خاصة #Visual Cو Visual Basicالمشاريع. VSProject2كما يوفر الوصول إلىDTEوProjectالكائنات طراز بيئة عامة خلالDTEوProjectخصائص.

معظم VSProject2تطبيق أساليب وخصائص إلى #Visual Cو Visual Basicالمشاريع. ‏‏ استثناء الوحيد هو Importsالخاصية التي تنطبق على Visual Basicوتوفير الوصول إلى Importsالكائن. لمزيد من المعلومات، راجع كيفية القيام بما يلي: التعامل مع خصائص عمليات الاستيراد من مشاريع Visual Basic. Eventsالخاصية يوفر الوصول إلى أحداث خاصة المشروع مثل VSLangProjWebReferencesEventsو ReferencesEvents. تغطي مواضيع أخرى المهام معالجة الحدث. لمزيد من المعلومات، راجع استجابة إلى أحداث (Visual Basic و ‏‫#Visual C المشاريع).

توضح الخطوات التالية كيفية إنشاء برمجياً #Visual Cإطارات التطبيق باستخدام طراز التنفيذ التلقائي العامة. أساليب وخصائص VSProject2تُستخدم برمجياً عنصر تحكم مشروع تم إنشاؤه.

ملاحظة

قد تختلف مربعات الحوار وأوامر القائمة التى تشاهدها الان عن تلك الموصوفة في التعليمات اعتماداً على الإعدادات النشطة أو الإصدار الخاص بك. تم تطوير هذه الإجراءات من خلال "إعدادات تطوير عام" النشط. لتغيير الإعدادات الخاصة بك, اختر إعدادات الاستيراد و التصدير ضمن القائمة أدوات . لمزيد من المعلومات، راجع العمل مع إعدادات.

إلى استخدام كائن VSProject2 إلى التحكم C# المشاريع

  1. Visual Studioإضافة-in مشروع باستخدام إنشاء #Visual C.

  2. تشغيل القائمة مشروع، انقر فوق إضافة مرجع، انقر فوق علامة تبويب .NET تحديد VSLangProj VSLangProj2 VSLangProj80 ثم انقر فوق ‏‏موافق .

  3. قم بإنشاء مجلدين تشغيل جهاز الكمبيوتر الخاص بك:

    • < جذر التثبيت > \UserFiles\MyProjects\MyTestProject.

    • < جذر التثبيت > \UserFiles\MyKeyFiles.

      في هذا المثال، < جذر التثبيت > هو "C:".

  4. إضافة التالية استخدام عبارات إلى أعلى الملف يعيّن.cs.

    using VSLangProj;
    using VSLangProj2;
    using VSLangProj80;
    
  5. إضافة استدعاء الأسلوب التالي إلى أسلوب OnConnection.

    public void OnConnection(object application, 
    ext_ConnectMode connectMode, object addInInst, ref Array custom)
    {
        _applicationObject = (DTE2)application;
        _addInInstance = (AddIn)addInInst;
        CSVSProj2Manip(_applicationObject);
    }
    
  6. إضافة تعريف أسلوب CSVSProj2Manip مباشرةً أسفل أسلوب OnConnection.

    public void CSVSProj2Manip(DTE2 dte)
    {
    }
    
  7. قم بإضافة سطر التعليمة البرمجية التالية أعلى الملف.

    Solution2 soln = (Solution2)_applicationObject.Solution;
    String csTemplatePath;
    String csPrjPath;
    Project proj;
    VSProject2 vsproj;
    String webServiceRef;
    BuildManager bldMgr;
    
  8. استخدام AddFromTemplateإلى إنشاء #Visual Cالمشروع.

    • بناء الجملة من أجل الحصول على القوالب EnvDTE80.Solution2.GetProjectTemplate("WindowsApplication.zip", "CSharp"), أين الاسم " WindowsApplication.zip "يتم الحصول عليها من WindowsApplication.zip الملف موجود في \برنامج ملفات\ < جذر التثبيت > Microsoft ‏‫Visual Studio 8\Common7\IDE\ProjectTemplates\VisualBasic\Windows\1033 المجلد. للحصول على كافة Visual Studioأنواع يمكن العثور على هذه الملفات في المشروع < جذر التثبيت > \برنامج Files\Microsoft 8\Common7\IDE\ProjectTemplates\ ‏‫Visual Studio اللغة مجلد. "CSharp"تحديد أن هذا المشروع#Visual Cالمشروع.

    • للحصول على Visual Basicالمشروع تطبيقات Windows بناء الجملة EnvDTE80.Solution2.GetProjectTemplate("WindowsApplication.zip", "VisualBasic"). للحصول على Visual Basicالعثور على قوالب ملف zip تطبيقات Windows في المشاريع < جذر التثبيت > \ المجلد 8\Common7\IDE\ProjectTemplates\VisualBasic\Windows\1033 ملفات\Microsoft برنامج ‏‫Visual Studio.

    // Make sure you create the folders that 
    // make up the file path
    // on your computer. You can replace 
    // this with your own file path.
    csPrjPath = "C:\\UserFiles\\MyProjects\\MyTestProject";
    // Get the project template path for a C# windows 
    // application.
    csTemplatePath = soln.GetProjectTemplate
    ("WindowsApplication.zip", "CSharp");
    // Create a new windows application by using the 
    // template obtained above.
    soln.AddFromTemplate(csTemplatePath, csPrjPath,
     "Test2CSProj", false);
    
  9. قم بإضافة تعليمات برمجية التالية إلى عرض الاستخدام VSProject2أساليب.

    لإضافة خدمة ويب بطريقة برمجية إلى المشروع يجب استبدال النص النائب, <web service> في تعليمات برمجية مع محدد موقع المعلومات (URL) الخاص الفعلية ويب الخدمة. إلى موقع URL الخدمة ويب ، قم بفتح مشروع في Visual Studioبيئة التطوير المتكاملة (IDE). في القائمة " مشروع " انقر فوق " إضافة مرجع " . في مربع حوار إضافة المرجع، انقر فوق الارتباط دليل UDDI واستخدم الدليل العثور تشغيل الخدمة ويب.

    proj = soln.Projects.Item(1);
    // Cast the project as a VSProject2 object.
    vsproj = (VSProject2)proj.Object;
    // Add a reference to System.Security.dll.
    MessageBox.Show("Adding a reference 
    to System.Security.dll");
    // Remove the <version number> in the following path
    // and replace it with one of the version 
    // number folders that appear 
    // in <installation root>\WINDOWS\Microsoft.NET\Framework
    // folder
    vsproj.References.Add
    ("C:\\WINDOWS\\Microsoft.NET\\Framework\\<version number>\\System.Security.dll");
    // Create a Web references folder.
    MessageBox.Show("Creating a Web references folder.");
    vsproj.CreateWebReferencesFolder();
    // Add a Web reference to the folder.
    MessageBox.Show("Adding a Web reference.");
    // Replace the placeholder, <web service>, with a
    // Web service URL.
    webServiceRef = "<web service>";
    vsproj.AddWebReference(webServiceRef);
    bldMgr = vsproj.BuildManager;
    Array monikers = null;
    // String moniker = null;
    String msg = null;
    Object obj = bldMgr.DesignTimeOutputMonikers;
    if (obj != null)
    {
        try
        {
            monikers = (System.Array)obj;
            foreach(String tempmoniker in monikers)
            {
                msg += bldMgr.BuildDesignTimeOutput
    (tempmoniker) + "\n";
            }
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        MessageBox.Show("The build design-time output is:" 
    + "\n"  + msg);
    }
    // Change the MyHTML file name by using GetUniqueFilename.
    MessageBox.Show("Adding an HTML page named 'MyHTML'...");
    String itemTemplatePath = soln.GetProjectItemTemplate("HTMLPage",
     "CSharp");
    proj.ProjectItems.AddFromTemplate
    (itemTemplatePath, "MyHtml");
    MessageBox.Show("Renaming MyHtml' to 'MyTesthtml.htm'...");
    vsproj.Project.ProjectItems.Item("MyHtml").Name =
     vsproj.GetUniqueFilename(proj, "MyTesthtml", "htm");
    // Generate a key-pair file.
    MessageBox.Show("Generating a key-file...");
    vsproj.GenerateKeyPairFiles("C:\\UserFiles\\MyKeyFiles
    \\MyKeyText2.bin", "0");
    

    يستخدم أسلوب VBVSProj2Manip VSProject2الكائن إلى:

    • إضافة مرجع إلى النظام.الأمان.dll باستخدام References.

    • إنشاء مجلد ويب مراجع باستخدام CreateWebReferencesFolder.

    • إضافة مرجع ويب باستخدام AddWebReference.

    • عرض الألقاب وقت التصميم التصنيع بواسطة استخدام أساليب عبر BuildManagerالخاصية.

    • إعادة تسمية مشروع جديد العنصر باستخدام GetUniqueFilenameالأسلوب. يضيف أسلوب CSVSProj2Manip عنصر المشروع باستخدام AddFromTemplate.

    • إنشاء ملف المفتاح ينشئ ازدواجًا باستخدام GenerateKeyPairFilesالأسلوب.

    يسرد المقطع المثال في إكمال تعليمات برمجية بما في ذلك حظر محاولة catch لأسلوب بأكمله.

  10. إلى إنشاء الوظيفة الإضافية وانقر فوق بنية الحل بنية قائمة.

  11. في Visual StudioIDE فتح Visual Basicأو #Visual Cالمشروع.

  12. في القائمة أدوات، انقر فوق إضافة - إدارة وتحديد الخاص بك إضافة - من إضافة - في إدارة مربع حوار. انقر فوق ‏‏موافق إلى تشغيل الوظيفة الإضافية.

  13. عرض الملف ينشئ ازدواجًا المفتاح الذي أنشأه في مجلد \UserFiles\MyKeyFiles < جذر التثبيت >باستخدام Sn.exe (أداة الاسم القوي).

مثال

المثال التالي هو أساسية Visual Studioوظيفة إضافية تقوم بإنشاء #Visual Cمشروع و تعالج باستخدام الخصائص والأساليب الخاصة VSProject2الكائن.

using System;
using System;
using Extensibility;
using EnvDTE;
using EnvDTE80;
using System.Windows.Forms;
using VSLangProj;
using VSLangProj2;
using VSLangProj80;
public void OnConnection(object application, 
ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;
    CSVSProj2Manip(_applicationObject);
}
public void CSVSProj2Manip(DTE2 dte)
{
    try
    {
        Solution2 soln = (Solution2)_applicationObject.Solution;
        String csTemplatePath;
        String csPrjPath;
        Project proj;
        VSProject2 vsproj;
        String webServiceRef;
        BuildManager bldMgr;
        // Make sure you create the folders that make up the file path
        // on your computer.
        // You can replace this with your own file path.
        csPrjPath = "C:\\UserFiles\\MyProjects\\MyTestProject";
        // Get the project template path for a C# windows application.
        csTemplatePath = soln.GetProjectTemplate
("WindowsApplication.zip", "CSharp");
        // Create a new Windows application by using the template 
        // obtained above.
        soln.AddFromTemplate(csTemplatePath, csPrjPath,
 "Test2CSProj", false);
        proj = soln.Projects.Item(1);
        // Get a reference to the VSProject2 object.
        vsproj = (VSProject2)proj.Object;
        // Add a reference to System.Security.dll.
        MessageBox.Show("Adding a reference to System.Security.dll");
        // Remove the <version number> in the following path
        // and replace it with one of the version 
        // number folders that appear 
        // in <installation root>\WINDOWS\Microsoft.NET\Framework
        // folder
        vsproj.References.Add
("C:\\WINDOWS\\Microsoft.NET\\Framework\\<version number>\\System.Security.dll");
        // Create a Web references folder.
        MessageBox.Show("Creating a Web references folder.");
        vsproj.CreateWebReferencesFolder();
        // Replace the placeholder, <web service>, with a
        // Web service URL.
        MessageBox.Show("Adding a Web reference.");
        // Replace the placeholder, <web service>, with a
        // Web service URL.
        webServiceRef = "<web service>";
        vsproj.AddWebReference(webServiceRef);
        bldMgr = vsproj.BuildManager;
        Array monikers = null;
        // String moniker = null;
        String msg = null;
        Object obj = bldMgr.DesignTimeOutputMonikers;
        if (obj != null)
        {
            try
            {
                monikers = (System.Array)obj;
                foreach(String tempmoniker in monikers)
                {
                    msg += bldMgr.BuildDesignTimeOutput(tempmoniker) 
+ "\n";
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            MessageBox.Show("The build design-time output is:" + "\n"  
+ msg);
        }
        // Change the MyHTML file name by using GetUniqueFilename.
        MessageBox.Show("Adding an HTML page named 'MyHTML'...");
        String itemTemplatePath =
 soln.GetProjectItemTemplate("HTMLPage", "CSharp");
        proj.ProjectItems.AddFromTemplate(itemTemplatePath, "MyHtml");
        MessageBox.Show("Renaming MyHtml' to 'MyTesthtml.htm'...");
        vsproj.Project.ProjectItems.Item("MyHtml").Name =
 vsproj.GetUniqueFilename(proj, "MyTesthtml", "htm");
        // Generate a key-pair file.
        MessageBox.Show("Generating a key-file...");
        vsproj.GenerateKeyPairFiles
("C:\\UserFiles\\MyKeyFiles\\MyKeyText2.bin", "0");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}
Imports System
Imports Microsoft.VisualStudio.CommandBars
Imports Extensibility
Imports EnvDTE
Imports EnvDTE80
Imports VSLangProj
Imports VSLangProj2
Imports VSLangProj80
Public Sub OnConnection(ByVal application As Object, _
 ByVal connectMode As ext_ConnectMode, ByVal addInInst As Object, _
 ByRef custom As Array) Implements IDTExtensibility2.OnConnection
    _applicationObject = CType(application, DTE2)
    _addInInstance = CType(addInInst, AddIn)
    CSVSProj2Manip(_applicationObject)
End Sub
Sub CSVSProj2Manip(ByVal dte As DTE2)
    Try
        Dim soln As Solution2 = CType(_applicationObject.Solution, _
         Solution2)
        Dim csTemplatePath As String
        Dim csPrjPath As String
        Dim proj As Project
        Dim vsproj As VSProject2
        Dim webServiceRef As String
        Dim bldMgr As BuildManager
        ' Create this or your own file path on your computer.
        ' The file path must exist before you run this add-in.
        csPrjPath = "C:\UserFiles\MyProjects\MyTestProject"
        ' Get the project template path for a C# windows application.
        csTemplatePath = soln.GetProjectTemplate _
        ("WindowsApplication.zip","CSharp")
        ' Create a new Windows Application
        ' using the template obtained above.
        soln.AddFromTemplate(csTemplatePath, csPrjPath _
        , "Test2CSProj", False)
        proj = soln.Projects.Item(1)
        ' Cast the project to a VSProject2.
        vsproj = CType(proj.Object, VSProject2)
        ' Add a reference to System.Security.dll.
        MsgBox("Adding a reference to System.Security.dll")
        ' Remove the <version number> in the following path
        ' and replace it with one of the version 
        ' number folders that appear 
        ' in <installation root>\WINDOWS\Microsoft.NET\Framework
        ' folder
        vsproj.References.Add _
        ("C:\WINDOWS\Microsoft.NET\Framework\<version number>\System.Security.dll")
        ' Create a Web references folder.
        MsgBox("Creating a Web references folder.")
        vsproj.CreateWebReferencesFolder()
        ' Replace the placeholder, <web service>, with a
        ' web service URL.
        webServiceRef = "<web service>"
        MsgBox("Adding a Web reference.")
        vsproj.AddWebReference(webServiceRef)
        bldMgr = vsproj.BuildManager
        Dim monikers As String() = Nothing
        Dim moniker As String = Nothing
        Dim msg As String = ""
        Dim obj As Object = bldMgr.DesignTimeOutputMonikers
        If Not obj Is Nothing Then
            Try
                monikers = CType(obj, String())
                For Each moniker In monikers
                    msg &= bldMgr.BuildDesignTimeOutput(moniker) + vbCr
                Next
            Catch ex As System.Exception
                MsgBox(ex.ToString)
            End Try
            MsgBox("The build design-time output is:" + vbCr + msg)
        End If
        ' Use the UniqueFilename to rename a new project item.
        MsgBox("Adding an HTML page called 'MyHTML'...")
        Dim itemTemplatePath As String = _
        soln.GetProjectItemTemplate("HTMLPage", "CSharp")
        proj.ProjectItems.AddFromTemplate(itemTemplatePath, "MyHtml")
        MsgBox("Renaming MyHtml' to 'MyTesthtml.htm'...")
        vsproj.Project.ProjectItems.Item("MyHtml").Name = _
        vsproj.GetUniqueFilename(proj, "MyTesthtml", "htm")
        ' Generate a key-pair file.
        vsproj.GenerateKeyPairFiles _
        ("C:\UserFiles\MyKeyFiles\MyKeyText2.bin", "0")
    Catch ex As System.Exception
        MsgBox(ex.ToString)
    End Try
End Sub

التحويل البرمجي للتعليمات البرمجية

إلى يحول برمجياً هذه التعليمة البرمجية إنشاء جديد Visual Studioإضافة-in المشروع واستبدالها رمز أسلوب OnConnection مع تعليمات برمجية الموجودة في المثال. لمزيد من المعلومات حول كيفية إلى تشغيل وظيفة الإضافية "، راجع كيفية: عنصر تحكم الوظائف الإضافية مع إدارة الوظائف الإضافية.

راجع أيضًا:

المبادئ

مقدمة إلى كائن VSProject2

موارد أخرى

توسيع Visual Basic و ‏‫#Visual C المشاريع