C#编程之SharpSVN的官方使用方法参考
小标 2019-03-13 来源 : 阅读 1991 评论 0

摘要:本文主要向大家介绍了C#编程之SharpSVN的官方使用方法参考,通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。

本文主要向大家介绍了C#编程之SharpSVN的官方使用方法参考,通过具体的内容向大家展示,希望对大家学习C#编程有所帮助。

C#编程之SharpSVN的官方使用方法参考

SharpSvn Tutorial

Table Of Contents

Introduction and Prerequisites

Setting the project up

Getting Started With the SharpSVN API

Using the SvnClient Class to Perform Some Common SVN operations

svn checkout

svn commit

svn proplist

svn propset

Getting Help

 Introduction and Prerequisites
This tutorial is meant to serve as a "getting started" type of guide to the SharpSvn API.  It does not cover every aspect of SharpSvn but rather serves as a foothold so you can begin writing useful code rather than spending hours experimenting just to understand the basics.  It should teach you how to use some of the most important classes/methods in the API.  Additionaly, after reading through this tutorial it is my hope that you will have a sense of where to turn if you should happen to get stuck.
  
  This walkthrough assumes a few things. 
  First, it assumes that you know the basics of how to work with an SVN
  repository.  Second, it assumes that you have a working SVN
  repository to test against.  If either of these items are not true
  then you may have some difficulty following along.  An excellent
  place to get started with Subversion is by reading through the official
  documentation at
  
  //svnbook.red-bean.com/en/1.4/svn-book.html.
  
  Something else you should know before getting started is that this
  tutorial uses Visual Studio 2010, Visual C#, and version 4.0 of the .NET
  framework.  You may still be able to follow along as long as you
  can translate what is going on here to older versions of Visual
  Studio/.NET languages.  I have tried to avoid using any .NET 4.0
  specifics here to make any such translations relatively painless.
  
  Finally, if you have any questions/comments/corrections please let me
  know.  I can be contacted at bctelloat gmail dot
  com.

  Setting the Project
  Up
So to get started we will walk through
  setting up a windows forms project to access the SharpSvn API. 
  Although I‘m sure that most of the individuals reading this
  documentation are well aware of how to set up a project in Visual Studio
  I will be walking through it anyways to ensure everyone is on the same
  page.

Open Visual Studio and select
   File->New->Project.  In the ‘New Project‘ dialog, select Visual
   C# under the ‘Installed Templates‘ menu and Windows Forms
   Application from the center menu.

   

In the ‘Name‘ field type MySVNClient.

   

In the ‘Solution‘ pulldown select ‘Create new
   solution‘.  What you have now should look something like this:
   

   

Click OK.

   

Now you are going to need to import the SharpSVN
  assemblies into your project which will give you access to all of the
  datatypes and methods available in the SharpSVN API.

Download the appropriate SharpSVN binaries
   package from
   
   //sharpsvn.open.collab.net.  The various packages are located under
   the ‘Download and Support‘ tab.
Unzip the package onto your hard drive. 
   This will create a directory named "SharpSVN."
Open your project in Visual Studio.
In the Solution Explorer expand the node
   corresponding to your project and right click the "References"
   sub-folder.  Click ‘Add Reference...‘
   
   
Now in the ‘Add Reference‘ dialog which
   appears, click the Browse tab and browse to the unzipped "SharpSvn"
folder.  In that folder you will find the DLL files that you
   will need to access the SharpSVN methods.
Select ‘SharpSvn.dll‘ and click
   ‘OK‘.
   
   

You now have access to everything in the SharpSVN
  namespace and some extra goodies (which we won‘t be dealing with here) as well.  If you want to see
  everything that you can now access, just open up the Object Browser by
  clicking View->Object Browser and right clicking the arrow next to the
  SharpSvn node.
  
  As you can see, there are several other namespaces included in the
  SharpSvn assembly but we will not be using them for this walkthrough.
As a final setup step in getting this project set
  up, we will get the main form laid out properly.

Open the form ‘Form1‘ in design view.  At
   this point all you should see is a blank form with the title Form1.

   

In the Solution Explorer rename Form1.cs to
   SvnClient.cs

   

Change the properties of the form as follows:

Name: SvnClient

    

Text: SVN Client

    

Size: 325,120

    

   

Add a label to the form with the following
   properties:

Name: lblPrompt

    

Location: 38,9

    

Text: Select an SVN
    command to run and then click ‘Go‘.

    

   

Add a ComboBox to the form and change it‘s
   properties as follows:

Name: cbCmdList

    

Location: 92,25

    

Size: 121,21

    

   

Add two buttons to the form and set their
   properties as follows:

Name (first button):
    btnGo

    

Location: 81,52

    

Size: 44,23

    

Text: Go

    

Name (second button):
    btnQuit

    

Location: 149,52

    

Size: 75,23

    

Text: Quit

    

   

Now double click the ‘Quit‘ button which
   should bring you into the
   btnQuit_Click method.  In the body of the function type:

Application.Exit();

    

   

You should now have a form which looks
   similar to the image below.  It doesn‘t have to look exactly
   the same but if you want to be able to copy and paste the code in
   this walkthrough you should at least make sure the names of your
   controls match what has been listed above.
   

   

Save your project, make sure it builds, and
   that the Quit button actually closes the application.

   

  Getting Started With the SharpSVN API
Now that you have the baseline form for our demo
  project set up we can get to working with the API.  The first thing you
  need to know is that the SharpSvn
  namespace contains most of the types you will need to write a basic
  client.  If you have added the SharpSvn.dll assembly to your
  project as outlined above, you can access the SharpSvn namespace by
  simply typing using SharpSvn;
  at the top of your code files.
  
  
The second thing you need to know is that the
  SharpSvn.SvnClient
  class encapsulates the functionality of a Subversion client.  If
  there is something that you can do with a Subversion client program then
  it is likely something you can do via some function in the SvnClient
  class.  From this point on our demo program is going to proceed by
  walking through how to perform some of the basic functions of an svn
  client application by using the SvnClient class. 

  Using
  SvnClient to Perform Some Common SVN Operations
svn checkout
The first example is how to perform the svn
  checkout operation using the SharpSvn method
  SvnClient.CheckOut. We‘ll start by creating a new form which will
  have fields into which the user can type data for the ‘svn checkout‘
  operation.  It will also have a button labeled ‘checkout‘ which
  will gather all of the user specified data and perform the actual
  checkout.

In Visual Studio, click Project->Add Windows
   Form...

   

In the ‘Add New Item‘ dialog which appears,
   make sure that ‘Windows Form‘ is selected and enter ‘frmCheckout.cs‘
   in the ‘Name:‘ field

   

Click ‘Add‘

   

Add three labels to the form:

Name (first label): lblRepoURI

    

Location: 13, 12

    

Text: Repository URI:

    

Name (second label):
    lblLocalPath

    

Location: 13, 44

    

Text: Local Path

    

Name (third label): lblRevision

    

Location: 12,47

    

Text: Revision:

    

   

Add three text boxes to the form:

Name (first text box): tbRepoURI

    

Location: 102, 12

    

Size: 247,20

    

Name (second text box):
    tbLocalPath

    

Location: 102, 44

    

Size: 247, 20

    

Name (third text box): tbRevision

    

Location: 102, 71

    

Size: 75,20

    

   

Add two buttons to the form:

Name (first button): btnCheckout

    

Location: 102, 97

    

Text: Checkout

    

Name (second button): btnCancel

    

Location: 183, 97

    

Text: Cancel

    

   

Now set the properties of frmCheckout itself
   as follows:

CancelButton: btnCancel

    

Size: 377, 172

    

Text: svn checkout

    

   

The new form should look something like this:
   

   

Now open the SvnClient form and click the
   ComboBox cbCmdList.

   

In the properties window for cbCmdList open
   the ‘Items‘ collection editor.

   

Enter the string ‘svn checkout‘ on its own
   line and click OK.

   

Double click the ‘Go‘ button which should
   bring you to the code editor for the method btnGo_Click().

   

Change the btnGo_Click method so it matches
   the following code:
   
   private void btnGo_Click(object sender,
    EventArgs e){   
   object command = cbCmdList.SelectedItem;    if(command == null) //If the combo box was empty    {        MessageBox.Show("Please select an SVN
    command from the pull down menu.");        return;    }    switch (command.ToString())    {        case "svn checkout": //Open the svn
    checkout box            frmCheckout
    checkOutFrm = new frmCheckout();           
    checkOutFrm.ShowDialog();            break;        default: //User probably typed
    something invalid           
    MessageBox.Show("Please select a valid SVN command from the pull
    down menu.");            break;    }
   }

   

Now save and run your project.  Select
   ‘svn checkout‘ from the combo box on the main form, click ‘Go‘ and
   make sure it opens the ‘svn checkout‘ form.

   

Now that we have the checkout form laid out
   and working the way we want, we can add some SVN functionality to it
   (finally!).  Start by opening the frmCheckout.cs form in design
   view and double clicking the ‘Checkout‘ button.  This should
   bring you to the code editor.

   

At the top of the code for frmCheckout type

 using SharpSvn;

    

   

Change the method btnCheckout_Click() so that
   it looks like the following:
 
   private void btnCheckout_Click(object sender,
   EventArgs e)
   {
   
    if (tbLocalPath.Text.Length == 0|| tbRepoURI.Text.Length == 0)
    {
        MessageBox.Show("The ‘Repository URI‘
   and ‘Local Path‘ fields cannot be empty.");
        return;
    }
   
    //SvnUpdateResult provides info about what happened during a
   checkout
    SvnUpdateResult result;
   
    //we will use this to tell CheckOut() which revision to fetch
    long revision;
   
    //SvnCheckoutArgs wraps all of the options for the ‘svn
   checkout‘ function
    SvnCheckOutArgs args = new SvnCheckOutArgs();
   
    //path is the path where the local working copy will end up
    string path = tbLocalPath.Text;
   
    if (long.TryParse(tbRevision.Text,out revision))
    {
        //set the revision number if the user
   entered a valid number
        args.Revision = new
   SvnRevision(revision);
    }
    //if args.Revision is not set, it defaults to fetch the HEAD
   revision.
    else MessageBox.Show("Invalid Revision number, defaulting to
   HEAD");
   
    //the using statement is necessary to ensure we are freeing
   up resources
    using (SvnClient client = new SvnClient())
    {
        try
        {
           
   //SvnUriTarget is a wrapper class for SVN repository URIs
            SvnUriTarget
   target = new SvnUriTarget(tbRepoURI.Text);
   
            //this is the
   where ‘svn checkout‘ actually happens.
            if
   (client.CheckOut(target, path, args, out result))
               
   MessageBox.Show("Successfully checked out revision " +
   result.Revision + ".");
        }
        catch (SvnException se)
        {
           
   MessageBox.Show(se.Message,
            "svn checkout
   error",
           
   MessageBoxButtons.OK,
           
   MessageBoxIcon.Error);
        }
        catch (UriFormatException ufe)
        {
           
   MessageBox.Show(ufe.Message,
            "svn checkout
   error",
           
   MessageBoxButtons.OK,
           
   MessageBoxIcon.Error);
        }
    }
   }
   
   This is the actual code which
   performs the checkout operation so lets take a moment to make sure
   we understand what‘s going on here. 
   
   First, the method it all centers around is
   SvnClient.CheckOut().  Notice that the overload we have
   used here takes four
   arguments.  The first argument is an
   SvnUriTarget which is a wrapper class which converts standard
   URI‘s to a format that the Subversion libraries can use.  The
   second is a string which should be a valid path on the local
   machine.  This is the path to which the working copy will be
   checked out.  The third argument is of type
   SvnCheckoutArgs which is a wrapper class for the arguments to
   the ‘svn checkout‘ function.  Notice how we specified which
   revision we wanted to checkout by modifying the revision property of
   args and then passing it as the third argument to
   CheckOut().  Many of the functions in SharpSVN have their
   own argument classes that are used in the same manner.  The
   final argument is an output argument of type SvnUpdateResult. 
   This class can provide you with basic information about what
   happened during a ‘svn checkout‘ operation.  Again, several of
   the SharpSVN methods have similar result types which can be used to
   retrieve information about the result of a method call.
   
   The CheckOut method has several different overloads which require
   fewer arguments in case you decide you don‘t need one of them for
   some reason.  CheckOut(4) has been shown here because it is the
   overload with the highest arity so knowing how to use this version
   means you can use all of them.
   
   The next item of interest is the
   using(SvnClient client = new SvnClient())
   block.  We need to include this to
   ensure that any memory/network connections which get allocated to
   client are freed up as quickly as possible.  What it
   does, for those who are unfamiliar with
   using statements is defines a
   limited scope during which client will exist.  Once
   the using
   block is exited, client goes out of scope and is
   immediately disposed of via a call to Dispose().
   
   Finally, notice that we are catching the SvnException class here. 
   There are a considerable number of different Exception classes in
   SharpSVN but this one is the base class for all of them.  It
   gives you access to high level diagnostic information such as human
   readable error messages, operating system error codes, and even the
   error codes generated by the native subversion libraries that
   SharpSVN runs on top of.  If you need more specific
   information, you should look to the more specific exception classes. 
   We will not be going over those here however.

   

Now go ahead and save your project and make
   sure that it builds.  Run the project in Debug mode, select
   ‘svn checkout‘ and click ‘Go‘
   
   HINT: If you are like me and running this project
   in Visual Studio 2010 with the .NET 4.0 framework, you likely just
   encountered a nasty FileLoadException saying something like
   "Mixed mode assembly is built against version ‘v2.0.50727‘ of the
   runtime and cannot be loaded in the 4.0 runtime without additional
   configuration information."  To fix this, you need to
   create an XML configuration file telling Visual Studio to make the
   runtime .NET 2.0 compatible.   This file needs to be
   called MySvnClient.exe.config and should be saved to your projects
   debug/bin folder.  For example, on my machine I have to save it
   as
   
   C:\Users\Brady\My
   Documents\Visual Studio
   2010\Projects\MySvnClient\MySvnClient\bin\Debug\MySvnClient.exe.config
   
   The content of this file should be as follows:
   
   
   
       
   

   

   
   Now restart the SvnClient
   application.  Select ‘svn checkout‘ and click ‘Go‘ again and
   you should not receive that exception any longer.

   

Once you have the svn checkout form
   displayed, enter a valid SVN repository URI in the top text box and
   a valid local path in the middle text box (SharpSvn will create a
   directory if it doesn‘t exist).  Click Checkout.

   

Click ‘OK‘ in the box informing you that the
   HEAD revision will be checked out.

   

Depending on the size of your repository
   there may be a delay but once all the files have been downloaded and
   checked out, you will receive a confirmation window telling you
   which revision was checked out.  Navigating to the path you
   entered should confirm that the files have all been downloaded
   succesfully!

   

svn commit
To demonstrate how to use SvnClient.Commit
  we will be adding another form to the MySvnClient program.  This form
  will also gather data from the user and then perform the commit
  operation once a ‘commit‘ button is clicked.

Lets start adding the new form to our project
   by clicking Project->Add Windows Form...  Now make sure that
   ‘Windows Form‘ is selected and name it frmCommit.cs.  Click
   ‘OK‘

   

Add an OpenFileDialog to the form and set its
   properties as follows:

Name: ofdFileSelector

    

CheckFileExists: True

    

CheckPathExists: True

    

FileName: ""

    

   

Add two labels to the form and set their
   properties as follows:

Name (first label):
    lblFileName

    

Location: 12, 20

    

Text: File:

    

Name (second label):
    lblMessage

    

Location: 12, 42

    

Text: Message:

    

   

Add two text boxes to the form and set their
   properties as follows:

Name (first text box):
    tbFileName

    

Location: 71, 13

    

Size: 238, 20

    

Name (second text box):
    tbMessage

    

Location: 71, 39

    

Size: 319,20

    

   

Add three buttons to the form and set their
   properties as follows:

Name (first button):
    btnBrowse

    

Location: 315, 12

    

Text: Browse...

    

Name (second button):
    btnCommit

    

Location: 109,65

    

Text: Commit

    

Name (third button):
    btnCancel

    

Location: 206, 65

    

Text: Cancel

    

   

Set the properties of frmCommit itself as
   follows:

CancelButton: btnCancel

    

Size: 418, 138

    

Text: Svn Commit

    

   

That is the entire layout for the checkout
   form.  It should look something like this:
   

   

Now that you have the layout done lets add
   the commit form to our list of svn commands in the main form. 
   Open SvnClient.cs in the design window and open the Items property
   editor for the ComboBox cbCmdList.

   

On its own line type:
   
   svn commit
   
   and click ‘OK‘

   

Double click the ‘Go‘ button to bring you
   back to the btnGo_Click method.

   

In the switch statement you are going to add
   some code between the "svn checkout" case and the default case. 
   The final code should look  like the following (new code
   displayed in bold font and old code shortened to ... for brevity):
   
   case "svn checkout": //Open the svn checkout form
    ...
   case "svn commit": //Open the svn commit form
    frmCommit commitForm = new frmCommit();
    commitForm.ShowDialog();
    break;
   default:
    ...

   

Save your project, run it, and make sure that
   you can get to the new commit form from the main form.  Quit
   the program.

   

Open up frmCommit in design view and double
   click the ‘Browse...‘ button to bring you to the code for the
   btnBrowse_Click method.

   

 Add this code as the body of
   btnBrowse_Click()
   
   if (ofdFileSelector.ShowDialog() ==
   DialogResult.OK)
   {
    tbFileName.Text = ofdFileSelector.FileName;
   }

   

Bring frmCommit back up in design view and
   double click the ‘Commit‘ button to bring up the code for the
   btnCommit_Click method.  Now we are going to be inserting the
   code that will actually be committing our repository.

   

The first thing we are going to do is add
   another assembly to our project.  The assembly name is
   SharpSvn.UI.  It is important to us because it contains the
   SharpSvn.UI namespace. 
   This namespace contains code that makes handling authentication
   extremely easy.  To add the assembly, follow the same steps you
   used to add the SharpSvn.dll assembly but instead of choosing
   SharpSvn.dll, choose SharpSvn.UI.dll instead (they should be in the
   same directory).

   

At the top of the code for frmCommit.cs add
   the following two lines of code:
   
   using SharpSvn;
   using SharpSvn.UI;

   

Now go back to the body of the
   btnCommit_Click method and add the code shown in bold below:
   
    private void btnCommit_Click(object sender,
   EventArgs e)
   {
       if (tbFileName.Text.Length == 0)
    {
        MessageBox.Show("Please enter a valid
   filename in the ‘File:‘ box");
        return;
    }
   
    //This object allows us to provide options for ‘svn commit‘
    SvnCommitArgs args = new SvnCommitArgs();
   
    //This is how you specify a commit message.
    args.LogMessage = tbMessage.Text;
   
    //This is where results for ‘svn commit‘ are stored
    SvnCommitResult result;
   
    //again, remember to include the using statement with
   SvnClients
    using (SvnClient client = new SvnClient())
    {
        try
        {
            //Bind allows
   the client to prompt the user for authentication info.
           
   SvnUI.Bind(client, this);
   
            //This method
   is the equivalent of ‘svn commit‘
           
   client.Commit(tbFileName.Text, args, out result);
            if(result !=
   null)
               
   MessageBox.Show("Successfully commited revision " +
   result.Revision);
            else
               
   MessageBox.Show("No changes have been made to working copy since it
   was checked out.");
        }
        catch (SvnException se)
        {
           
   MessageBox.Show(se.Message + "Error: " + se.SvnErrorCode +
   Environment.NewLine,
            "svn commit
   error",
           
   MessageBoxButtons.OK,
           
   MessageBoxIcon.Error);
        }
    }
   }
   
   As before lets take a moment to step through this code and make sure
   that we understand everything that‘s going on here.
   
   The first thing to notice is the
   SvnCommitArgs class.  Just like
   SvnCheckoutArgs, this class allows you to specify options to the
   ‘svn commit‘ command.  In this example we are specifying the
   commit message by setting the LogMessage property of args
   before using it as an argument to
   Commit. 
   
   Also, notice that we are again using a class which allows us to
   analyze the results of a commit operation (SvnCommitResult). 
   If you are curious as to why we are checking whether or not
   result is null after the call to
   Commit it is because result is only initialized by
   Commit if there is at least one file which needs to be
   committed.  In other words, if your working copy has not been
   changed since you checked it out, Subversion/SharpSVN does not
   waste time sending anything over the network and thus nothing is
   changed in the repository.  SharpSvn indicates an unchanged
   repository by leaving result uninitialized.
   
   One of the big things to take away from this example is the call to
   SvnUI.Bind. 
   This method is helpful because it makes authentication so easy. 
   All you have to do is call
   Bind on
   an SvnClient
   and a Windows form before any call which could require
   authentication and automagically you have nice little
   username/password windows which pop up whenever you need to provide
   credentials.  This function also gives you the ability to use
   SSL certificates but we wont be getting into that here.
   
   Finally, the call to
   client.Commit is what actually executes the ‘svn commit‘
   operation.  Again, we use the catch all
   SvnException for error handling but feel free to use some of the
   more specific exception types if that does not fit your needs.

   

Now save your project, run it, open the svn
   commit window and make sure you can perform a commit to your
   repository.

   

svn proplist
Getting the list of SVN properties for a given
  node can be useful so lets work on an example of how to do that using
  SharpSVN.  Again we will demonstrate this functionality by adding
  ‘svn proplist‘ as a command to our MySvnClient program.  We will be
  creating a new form which asks the user to specify a file.  Once
  the user clicks a ‘Get Properties‘ button, all of the SVN properties for
  that node will be listed in a ListView control for all to see.

Once again lets start off with adding a new
   form to the MySvnClient project.  Click Project->Add Windows
   Form... Make sure that ‘Windows Form‘ is selected, name it
   frmPropList.cs and click ‘OK‘.

   

Open frmPropList.cs in the design view and
   add a ListView control with the following properties:

Name: lvProps

    

Columns:

Add a column named chFilePath with
     Text set to "File" and Width set to -2

     

Add a column named chPropName with
     Text set to "Name" and Width set to -2

     

Add a column name chPropValue with
     Text set to "Value" and Width set to -2

     

    

Location: 12, 88

    

Size: 369, 164

    

View: Details

    

   

Add a CheckBox to the form with the following
   properties:

Name: cbRecurse

    

Location: 13, 44

    

Text: Recurse into
    Directories?

    

   

Add two Label controls to the form and set
   their properties as follows:

Name (first label):
    lblFileURI

    

Location: 10, 13

    

Text: File URI:

    

Name (second label):
    lblProperties

    

Location: 13, 72

    

Text: Properties:

    

   

Add a text box to the form and set its
   properties as follows:

Name: tbFileURI

    

Location: 64, 12

    

Size: 318, 20

    

   

Add two buttons to the form and set their
   properties as follows:

Name (first button):
    btnGetProps

    

Location: 216, 40

    

Text: Get Properties

    

Name (second button):
    btnCancel

    

Location: 306, 40

    

Text: Cancel

    

   

Finally set the properties of frmPropList
   itself as follows:

CancelButton: btnCancel

    

Size: 417, 296

    

Text: svn proplist

    

   

Now that the form is laid out it should look
   something like the following:
   
   

   

Open SvnClient.cs in the code editor and
   update the switch statement in the btnGo_click method as follows:
   
   case "svn checkout": //Open the svn checkout form
    ...
   case "svn proplist": //Open the svn proplist form
    frmPropList propListForm = new frmPropList();
    propListForm.ShowDialog();
    break;
   default:
    ...

   

Save your project and run it.  Select
   ‘svn proplist‘ from the pulldown menu and click ‘Go‘ make sure that
   the proplist window opens.  Quit the application.

   

Open frmPropList in the design view and
   double click the ‘Get Properties‘ button to enter the method
   btnGetProps_Click().

   

Modify btnGetProps_Click as follows (don‘t
   forget to add the using SharpSvn statement
   at the top of the code):
  
   
   private void btnGetProps_Click(object sender,
   EventArgs e)
   {
       if (tbFileURI.Text.Length == 0)
    {
        MessageBox.Show("Please enter a valid
   file URI in the ‘File URI:‘ box.");
        return;
    }
   
    //Clear out the list view for the next list of properties
    lvProps.Items.Clear();
   
    //This will be the target file from which we are retrieving
   properties
     SvnTarget tgt = SvnTarget.FromString(tbFileURI.Text);
   
    //This collection will contain property collections for each
   node
   
   System.Collections.ObjectModel.Collection
   proplist;
   
    //This is where we can specify arguments to svn proplist
    SvnPropertyListArgs args = new SvnPropertyListArgs();
   
    if (cbRecurse.Checked == true)
    {
        //This will cause GetPropertyList to
   get properties recursively.
        args.Depth = SvnDepth.Infinity;
    }
   
    using (SvnClient client = new SvnClient())
    {
        try
        {
            //This method
   is what executes svn proplist
           
   client.GetPropertyList(tgt, args, out proplist);
   
            //Each
   SvnPropertyListEventArgs represents the prop. set for a node
            foreach
   (SvnPropertyListEventArgs node in proplist)
           
   {
               
   //Each SvnPropertyValue represents a single name/value property pair
               
   foreach (SvnPropertyValue propVal in node.Properties)
               
   {
                   
   ListViewItem entry = new ListViewItem(node.Path);
                   
   entry.SubItems.Add(propVal.Key);
                   
   entry.SubItems.Add(propVal.StringValue);
                   
   lvProps.Items.Add(entry);
               
   }
           
   }
            //refresh the
   column sizes
            for (int i =
   0; i < 3; i++) lvProps.Columns[i].Width = -2;
        }
        catch (SvnException se)
        {
           
   MessageBox.Show(se.Message + Environment.NewLine +
            "Error:" +
   se.SvnErrorCode,
            "svn proplist
   error",
           
   MessageBoxButtons.OK,
           
   MessageBoxIcon.Error);
        }
    }
   } 
   
   The first new thing that worth mentioning
   in this example is the use of the SvnTarget
   class.  SvnTarget is actually the abstract superclass for two concrete
   classes SvnPathTarget and SvnUriTarget, the former being SharpSvn‘s
   representation of a local file path and the latter being SharpSvn‘s
   representation of a remote URI.  Any method which takes an
   SvnTarget as an argument can take either of those two classes but
   the trick is to use SvnTarget‘s FromString method which creates the
   appropriate type for you based on the format of its input string. 
   For example if you say
   
   SvnTarget target =
   SvnTarget.FromString("//myproj.googlecode.com/svn/trunk/helloworld.c");
   
   then FromString would create an SvnTarget that can be used as a URI. 
   On the other hand if you typed 
   
   SvnTarget target = SvnTarget.FromString(@"C:\myproj.helloworld.c");
   
   then you would get an SvnTarget that can be used as a local file
   path.
   
   Next we should talk about the output Collection of
   SvnPropertyListEventArgs named proplist.  When you
   run
   GetPropertyList (the method which executes svn proplist), this
   collection is populated with one
   SvnPropertyListEventArgs instance for each node which has
   properties to report.  This instance holds the entire property
   set for its corresponding node.  Therefore, if you run the
   method against a single node you will only ever get a single item in
   proplist.  So how do you add additional nodes to your
   property query?  By recursion of course!  The way you
   normally tell functions in SharpSvn to recurse into directory nodes
   is by setting args.Depth
   to some member of the svnDepth enumeration.  For
   instance, in this example we have set it to svnDepth.Infinity
   which tells the GetPropertyList function to fully recurse into all
   directories. 
   GetPropertyList will now dive into all directories, adding a new
   entry to proplist for each file on the way. 
   Once you have your fully populated proplist you can go through each
   SvnPropertyListEventArgs and grab its property set.  Once
   you have the property set you can pull each key/value pair. 
   It‘s pretty confusing but if you study the two foreach loops
   above for
   long enough you will see that what you are dealing with is
   essentially a collection of collections.

   

Save your project and run it.  Select
   svn proplist from the pulldown menu.  Click ‘Go‘.  Enter a
   directory from your working copy or a repository into the ‘File
   URI:‘ box and click ‘Get Properties‘.  Click the ‘Recurse into
   directories‘ check box and click ‘Get Properties‘ again.  Make
   sure that the list view is populated with all the file properties
   beneath the directory you entered (if you haven‘t defined any svn
   properties for you repository you will need to do so before you see
   anything here).

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标编程语言C#.NET频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程