Example #1
0
        /// <summary>
        /// Opens the phone book.
        /// </summary>
        /// <param name="phoneBookPath">The path (including filename) of a phone book.</param>
        /// <remarks>This method opens an existing phone book or creates a new phone book if the file does not already exist.</remarks>
        /// <exception cref="System.ArgumentException"><paramref name="phoneBookPath"/> is an empty string or null reference (<b>Nothing</b> in Visual Basic).</exception>
        /// <exception cref="System.UnauthorizedAccessException">The caller does not have the required permission to perform the action requested.</exception>
        public void Open(string phoneBookPath)
        {
            if (string.IsNullOrEmpty(phoneBookPath))
            {
                ThrowHelper.ThrowArgumentException("phoneBookPath", Resources.Argument_StringCannotBeNullOrEmpty);
            }

            FileInfo file = new FileInfo(phoneBookPath);

            if (string.IsNullOrEmpty(file.Name))
            {
                ThrowHelper.ThrowArgumentException("phoneBookPath", Resources.Argument_InvalidFileName);
            }

            RasPhoneBookType phoneBookType = RasPhoneBookType.Custom;

            if (string.Equals(phoneBookPath, RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers), StringComparison.CurrentCultureIgnoreCase))
            {
                phoneBookType = RasPhoneBookType.AllUsers;
            }
            else if (string.Equals(phoneBookPath, RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User), StringComparison.CurrentCultureIgnoreCase))
            {
                phoneBookType = RasPhoneBookType.User;
            }

            this.Path          = file.FullName;
            this.PhoneBookType = phoneBookType;

            // Setup the watcher used to monitor the file for changes, and attempt to load the entries.
            this.SetupFileWatcher(file);
            this.Entries.Load();

            this.opened = true;
        }
Example #2
0
        /// <summary>
        /// Determines the full path (including filename) of the phone book.
        /// </summary>
        /// <param name="phoneBookType">The type of phone book to locate.</param>
        /// <returns>The full path (including filename) of the phone book.</returns>
        public static string GetPhoneBookPath(RasPhoneBookType phoneBookType)
        {
            Environment.SpecialFolder folder;
            switch (phoneBookType)
            {
            case RasPhoneBookType.User:
                folder = Environment.SpecialFolder.ApplicationData;
                break;

            case RasPhoneBookType.AllUsers:
                folder = Environment.SpecialFolder.CommonApplicationData;
                break;

            default:
                throw new NotSupportedException();
            }

            string path = Environment.GetFolderPath(folder);

            if (string.IsNullOrWhiteSpace(path))
            {
                ThrowHelper.ThrowInvalidOperationException(Resources.Exception_PathWasNotReturned);
            }

            return(Path.Combine(path, PhoneBookFilePath));
        }
Example #3
0
        public void Open(bool openUserPhoneBook)
        {
            RasPhoneBookType phoneBookType = RasPhoneBookType.AllUsers;

            if (openUserPhoneBook)
            {
                phoneBookType = RasPhoneBookType.User;
            }

            this.Open(RasPhoneBook.GetPhoneBookPath(phoneBookType));
            this.PhoneBookType = phoneBookType;
        }
Example #4
0
        public void OpenAllUsersProfileFileTest()
        {
            RasPhoneBookType expected = RasPhoneBookType.AllUsers;

            string phoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers);

            RasPhoneBook target = new RasPhoneBook();

            target.Open(phoneBookPath);

            RasPhoneBookType actual = target.PhoneBookType;

            Assert.AreEqual <RasPhoneBookType>(expected, actual);
        }
Example #5
0
        /// <summary>
        /// Determines the full path (including filename) of the phone book.
        /// </summary>
        /// <param name="phoneBookType">The type of phone book to locate.</param>
        /// <returns>The full path (including filename) of the phone book.</returns>
        /// <remarks><see cref="RasPhoneBookType.Custom"/> will always return a null reference (<b>Nothing</b> in Visual Basic).</remarks>
        public static string GetPhoneBookPath(RasPhoneBookType phoneBookType)
        {
            string retval = null;

            if (phoneBookType != RasPhoneBookType.Custom)
            {
                Environment.SpecialFolder folder = Environment.SpecialFolder.CommonApplicationData;
                if (phoneBookType == RasPhoneBookType.User)
                {
                    folder = Environment.SpecialFolder.ApplicationData;
                }

                retval = System.IO.Path.Combine(Environment.GetFolderPath(folder), PhoneBookFilePath);
            }

            return(retval);
        }
Example #6
0
        public static void CreateSstpVpn(string entryName, string serverName, RasPhoneBookType rasPhoneBookType = RasPhoneBookType.User, RasVpnStrategy rasVpnStrategy = RasVpnStrategy.SstpOnly)
        {
            var path = RasPhoneBook.GetPhoneBookPath(rasPhoneBookType);

            using (var phoneBook = new RasPhoneBook())
            {
                phoneBook.Open(path);

                if (phoneBook.Entries.Contains(entryName))
                {
                    MessageBox.Show(entryName + " recreated!", "Phonebook", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                    phoneBook.Entries.Remove(entryName);
                }

                var device = RasDevice.GetDevices().FirstOrDefault(d => d.Name.Contains("SSTP"));
                var entry  = RasEntry.CreateVpnEntry(entryName, serverName, rasVpnStrategy, device);

                entry.Options.RemoteDefaultGateway = false;

                phoneBook.Entries.Add(entry);
            }
        }
        /// <summary>
        /// Finds the RAS connection (VPN or dial up) and asynchronously dials that connection.
        /// </summary>
        /// <param name="rasConnectionName">The name of the RAS connection e.g. MyCompanyVPN</param>
        /// <param name="userName">The username to specify when dialing.</param>
        /// <param name="password">The password to specify when dialing.</param>
        /// <param name="timeout">Length of time until asynchronous dialing times out..</param>
        /// <param name="phoneBookType">The type of phone book e.g. current user or all users.</param>
        public void ConnectRasConnection(string rasConnectionName, string userName, string password, int timeout, RasPhoneBookType phoneBookType)
        {
            _rasPhoneBook.Open(GetRasConnectionsAddressBookPath(phoneBookType));
            if (!_rasPhoneBook.Entries.Contains(rasConnectionName))
            {
                throw new NullReferenceException(string.Format("No connection exists with the name {0}.", rasConnectionName));
            }
            _rasDialer.EntryName     = rasConnectionName;
            _rasDialer.PhoneBookPath = GetRasConnectionsAddressBookPath(phoneBookType);
            NetworkCredential credentials = new NetworkCredential(userName, password);

            _rasDialer.Timeout = timeout;
            _rasDialer.DialAsync();
        }
 /// <summary>
 /// Gets the path to the address book containing information about all the windows connections.
 /// </summary>
 /// <param name="phoneBookType">The type of phone book e.g. current user or all users.</param>
 /// <returns></returns>
 public string GetRasConnectionsAddressBookPath(RasPhoneBookType phoneBookType)
 {
     return(RasPhoneBook.GetPhoneBookPath(phoneBookType));
 }
Example #9
0
        /// <summary>
        /// Opens the phone book.
        /// </summary>
        /// <param name="phoneBookPath">The path (including filename) of a phone book.</param>
        /// <remarks>This method opens an existing phone book or creates a new phone book if the file does not already exist.</remarks>
        /// <exception cref="System.ArgumentException"><paramref name="phoneBookPath"/> is an empty string or null reference (<b>Nothing</b> in Visual Basic).</exception>
        /// <exception cref="System.UnauthorizedAccessException">The caller does not have the required permission to perform the action requested.</exception>
        public void Open(string phoneBookPath)
        {
            if (string.IsNullOrEmpty(phoneBookPath))
            {
                ThrowHelper.ThrowArgumentException("phoneBookPath", Resources.Argument_StringCannotBeNullOrEmpty);
            }

            FileInfo file = new FileInfo(phoneBookPath);
            if (string.IsNullOrEmpty(file.Name))
            {
                ThrowHelper.ThrowArgumentException("phoneBookPath", Resources.Argument_InvalidFileName);
            }

            RasPhoneBookType phoneBookType = RasPhoneBookType.Custom;
            if (string.Equals(phoneBookPath, RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers), StringComparison.CurrentCultureIgnoreCase))
            {
                phoneBookType = RasPhoneBookType.AllUsers;
            }
            else if (string.Equals(phoneBookPath, RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User), StringComparison.CurrentCultureIgnoreCase))
            {
                phoneBookType = RasPhoneBookType.User;
            }

            this.Path = file.FullName;
            this.PhoneBookType = phoneBookType;

            // Setup the watcher used to monitor the file for changes, and attempt to load the entries.
            this.SetupFileWatcher(file);
            this.Entries.Load();

            this.opened = true;
        }
Example #10
0
        public void Open(bool openUserPhoneBook)
        {
            RasPhoneBookType phoneBookType = RasPhoneBookType.AllUsers;
            if (openUserPhoneBook)
            {
                phoneBookType = RasPhoneBookType.User;
            }

            this.Open(RasPhoneBook.GetPhoneBookPath(phoneBookType));
            this.PhoneBookType = phoneBookType;
        }
Example #11
0
        /// <summary>
        /// Determines the full path (including filename) of the phone book.
        /// </summary>
        /// <param name="phoneBookType">The type of phone book to locate.</param>
        /// <returns>The full path (including filename) of the phone book.</returns>
        /// <remarks><see cref="RasPhoneBookType.Custom"/> will always return a null reference (<b>Nothing</b> in Visual Basic).</remarks>
        public static string GetPhoneBookPath(RasPhoneBookType phoneBookType)
        {
            string retval = null;

            if (phoneBookType != RasPhoneBookType.Custom)
            {
                Environment.SpecialFolder folder = Environment.SpecialFolder.CommonApplicationData;
                if (phoneBookType == RasPhoneBookType.User)
                {
                    folder = Environment.SpecialFolder.ApplicationData;
                }

                retval = System.IO.Path.Combine(Environment.GetFolderPath(folder), PhoneBookFilePath);
            }

            return retval;
        }