Click here to Skip to main content
15,891,976 members
Articles / Web Development / ASP.NET

Peer Collaboration - Inviting

Rate me:
Please Sign up or sign in to vote.
4.20/5 (4 votes)
1 May 20067 min read 27.2K   441   28  
Inviting People Near Me using Microsoft's Peer-to-Peer Collaboration technology in Windows Vista.
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using Peer.Collaboration;
using CommandLine;

namespace register
{
    class Arguments
    {
        [Argument(ArgumentType.AtMostOnce, HelpText = "application Guid")]
        public string id;
        [Argument(ArgumentType.AtMostOnce, HelpText = "application name")]
        public string name;
        [Argument(ArgumentType.AtMostOnce, HelpText = "application path")]
        public string path;
        [Argument(ArgumentType.AtMostOnce, HelpText = "application command-line arguments [optional]")]
        public string args;
        [Argument(ArgumentType.AtMostOnce, DefaultValue = PeerPublicationScope.All, HelpText = "visibility to People Near Me or your Contacts")]
        public PeerPublicationScope scope;
        [Argument(ArgumentType.AtMostOnce, DefaultValue = PeerApplicationRegistrationType.CurrentUser, HelpText = "visible to All Users or just Current User")]
        public PeerApplicationRegistrationType type;
        [Argument(ArgumentType.AtMostOnce, HelpText = "unregister application")]
        public bool delete;
        [Argument(ArgumentType.AtMostOnce, HelpText = "Force re-registration")]
        public bool force;
    }

    class Program
    {
        static void Main(string[] args)
        {
            if (System.Environment.OSVersion.Version < new Version("6.0"))
            {
                Console.WriteLine("Windows Vista Required");
                return;
            }

            Arguments reg = new Arguments();
            if (Parser.ParseArgumentsWithUsage(args, reg))
            {
                PeerCollab collab = new PeerCollab();

                if (reg.delete == true)
                {
                    Unregister(collab, reg);
                }
                else
                {
                    if (reg.id == null) reg.id = Guid.NewGuid().ToString();
                    if (reg.name == null)
                    {
                        Console.WriteLine("Name is required when registering");
                        Console.WriteLine(Parser.ArgumentsUsage(typeof(Arguments)));
                    }
                    else if (reg.path == null)
                    {
                        Console.WriteLine("Path is required when registering");
                        Console.WriteLine(Parser.ArgumentsUsage(typeof(Arguments)));
                    }
                    else
                    {
                        FileInfo info = new FileInfo(reg.path);
                        if (info.Exists == false)
                            Console.WriteLine("File does not exist.");
                        else
                        {
                            reg.path = info.FullName;
                            if (reg.force) Unregister(collab, reg);

                            PeerApplicationRegistration app = new PeerApplicationRegistration(new Guid(reg.id), reg.name, reg.path);
                            app.ApplicationArguments = reg.args;
                            app.PublicationScope = reg.scope;
                            try
                            {
                                if (reg.type == PeerApplicationRegistrationType.CurrentUser)
                                    collab.ApplicationsRegisteredForCurrentUser.Register(app);
                                else
                                    collab.ApplicationsRegisteredForAllUsers.Register(app);
                            }
                            catch (PeerCollabException ex)
                            {
                                if (ex.ErrorCode == -2147467259) //0x80004005
                                    Console.WriteLine("Application already registered");
                                else
                                    Console.WriteLine(ex.Message);
                            }
                        }
                    }
                }
            }
        }

        private static void Unregister(PeerCollab collab, Arguments reg)
        {
            if (reg.id == null)
            {
                Console.WriteLine("ID required when deleting");
                Console.WriteLine(Parser.ArgumentsUsage(typeof(Arguments)));
            }
            else
            {
                PeerApplicationRegistration app = new PeerApplicationRegistration(new Guid(reg.id), string.Empty, string.Empty);
                try
                {
                    if (reg.type == PeerApplicationRegistrationType.CurrentUser)
                        collab.ApplicationsRegisteredForCurrentUser.Unregister(app);
                    else
                        collab.ApplicationsRegisteredForAllUsers.Unregister(app);
                }
                catch (PeerCollabException ex)
                {
                    if (ex.ErrorCode == -2147467259) // 0x80004005
                        Console.WriteLine("Application with this ID not found.");
                    else
                        Console.WriteLine(ex.Message);
                }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Canada Canada
Adrian Moore is the Development Manager for the SCADA Vision system developed by ABB Inc in Calgary, Alberta.

He has been interested in compilers, parsers, real-time database systems and peer-to-peer solutions since the early 90's. In his spare time, he is currently working on a SQL parser for querying .NET DataSets (http://www.queryadataset.com).

Adrian is a Microsoft MVP for Windows Networking.

Comments and Discussions