Example #1
0
        public static RDPData ParseRDPFile(string path)
        {
            path = FileUtilities.ResolvePath(path);
            if (!FileUtilities.FileExists(path))
            {
                throw new System.IO.FileNotFoundException("The supplied file path does not exist.", path);
            }

            RDPData data = new RDPData();

            data.Path = path;

            string fileContents = FileUtilities.ReadFile(path);

            foreach (string line in fileContents.Split(Environment.NewLine.ToCharArray()).Where(s => !string.IsNullOrWhiteSpace(s)))
            {
                string[] parts = line.Split(RDP_PART_SPLIT);
                if (parts.Length < 3)
                {
                    continue;
                }

                data.Properties.Add(parts[0], parts.Skip(2).Aggregate((t, s) => t + RDP_PART_SPLIT + s));
            }

            return(data);
        }
        private async void OpenRDP()
        {
            _openRDPCommand.Enabled = false;

            if (string.IsNullOrWhiteSpace(RDAddress))
            {
                MessageBox.Show($"The address cannot be empty.", "Error");
                return;
            }

            IEnumerable <RDPData> rdpDatas = RDPUtilities.ParseRDPFiles(app.Settings.RDPFileSearchPath);

            //Find RDPData by file name
            RDPData rdpData = RDPUtilities.GetRDPDataByFileName(rdpDatas, RDAddress);

            //Find RDPData by domain
            if (rdpData == null)
            {
                rdpData = RDPUtilities.GetRDPDataByKeyValue(rdpDatas, new KeyValuePair <string, string>("full address", RDAddress));
            }

            //Find RDPData by IP
            if (rdpData == null)
            {
                IPAddress ipAddress;
                string[]  addressParts = RDAddress.Split(':');
                if (addressParts.Length >= 1)
                {
                    IPAddress.TryParse(addressParts[0], out ipAddress);
                    if (ipAddress == null)
                    {
                        try
                        {
                            ipAddress = (await Dns.GetHostAddressesAsync(RDAddress)).First();
                        }
                        catch (System.Net.Sockets.SocketException e)
                        {
                        }
                    }
                    if (ipAddress != null)
                    {
                        string address = ipAddress.ToString();
                        if (addressParts.Length >= 2)
                        {
                            address += $":{addressParts[1]}";
                        }
                        rdpData = RDPUtilities.GetRDPDataByKeyValue(rdpDatas, new KeyValuePair <string, string>("full address", address));
                    }
                }
            }

            //Start the RDP session
            if (rdpData != null)
            {
                RDPUtilities.StartExistingRDP(rdpData.Path);
                if (app.Settings.ExitOnSuccess)
                {
                    Environment.Exit(0);
                }
            }
            else
            {
                MessageBoxResult result = MessageBox.Show($"Unable to find RDPfile for '{RDAddress}'.\nOpen new session?", "Information", MessageBoxButton.YesNo);
                if (result == MessageBoxResult.Yes)
                {
                    RDPUtilities.StartNewRDP(RDAddress);
                    if (app.Settings.ExitOnSuccess)
                    {
                        Environment.Exit(0);
                    }
                }
            }

            _openRDPCommand.Enabled = true;
        }