共用方式為


How to: Create a Console Application

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

This programming task describes how to create a console application in Microsoft Visual Studio 2005. The example displays the number of lists within a site collection.

Users must be administrators on the computer where the script is executed in order to run a console application in the context of Windows SharePoint Services.

To create a console application in Visual Studio 2005

  1. On the File menu in Visual Studio 2005, point to New and then click Project.

  2. In the New Project dialog box, select a language, and then select Windows in the Project Types box.

  3. In the Templates box, select Console Application.

  4. In the Location box, type the path to where to create the application, and then click OK.

  5. In Solution Explorer, right-click the References node, and then click Add Reference on the shortcut menu.

  6. On the .NET tab of the Add Reference dialog box, select Windows SharePoint Services in the list of components, and then click OK.

  7. In the .vb or .cs file, add a using directive for the Microsoft.SharePoint namespace, as follows.

    Imports Microsoft.SharePoint
    
    using Microsoft.SharePoint;
    
  8. Add the following code to the Main method in the .vb or .cs file.

    Overloads Sub Main(args() As String)
    
        Dim siteCollection As New SPSite("http://Server_Name")
        Dim sites As SPWebCollection = siteCollection.AllWebs
        Dim site As SPWeb
    
        For Each site In  sites
    
            Dim lists As SPListCollection = site.Lists
    
            Console.WriteLine("Site: " + site.Name + "  Lists: " 
               + lists.Count.ToString())
    
        Next site
    
        Console.WriteLine("Press ENTER to continue")
        Console.ReadLine()
    
    End Sub 'Main
    
    static void Main(string[] args)
    {
        SPSite siteCollection = new SPSite("http://Server_Name");
        SPWebCollection sites = siteCollection.AllWebs;
    
        foreach (SPWeb site in sites)
        {
            SPListCollection lists =  site.Lists;
    
            Console.WriteLine("Site: " + site.Name + "  Lists: " 
               + lists.Count.ToString());
        }
    
        Console.WriteLine("Press ENTER to continue");
        Console.ReadLine();
    }
    
  9. Click Start on the Debug menu or press F5 to run the code.

See Also

Concepts

Working with List Objects and Collections

Getting Started with Programmatically Customizing a SharePoint Web Site in Visual Studio

Security Validation and Making Posts to Update Data

Elevation of Privilege