Beispiel #1
0
 private void SearchSubDirectoriesRecursive(string subFolder, List <string> dirs) //recrusive func to loop on al sub-folders that were sent to it
 {
     try
     {
         ShowProgress?.Invoke("");                                                                                    //invoke progress event
         foreach (string folder in Directory.GetDirectories(subFolder, "*", SearchOption.TopDirectoryOnly).ToArray()) //loop on all top directoires
         {
             dirs.Add(folder);                                                                                        // add this folder to the dirs list
             SearchSubDirectoriesRecursive(folder, dirs);                                                             //send each directory to the search recursive func again - recursive
         }
     }
     catch { }
 }
Beispiel #2
0
        /// <summary>Upload files from the named path via FTP to the internet.</summary>
        public static void UploadFiles(string uploadMethod, string uploadSite, string username, string password, string localPath, List <Holder> selected, ShowProgress progress = null)
        {
            string url = uploadMethod + "://" + uploadSite;

            if (url.Last() != '/')
            {
                url += '/';
            }

            Cursor.Current = Cursors.WaitCursor;
            try
            {
                using (WebClient client = new WebClient())
                {
                    client.Credentials = new NetworkCredential(username.Normalize(), password.Normalize());

                    UploadFile(client, url, localPath, "index.html");

                    for (int h = 0; h < selected.Count; h++)
                    {
                        string key = selected[h].Key;

                        DirectoryInfo di = new DirectoryInfo(Path.Combine(localPath, key));

                        // Create a directory on FTP site:
//					    WebRequest wr = WebRequest.Create(url + key);
//						wr.Method = WebRequestMethods.Ftp.MakeDirectory;
//						wr.Credentials = client.Credentials;
//						wr.GetResponse();

                        FileInfo[] files = di.GetFiles("*.html");
                        for (int i = 0; i < files.Count(); i++)
                        {
                            UploadFile(client, url, Path.Combine(localPath, key), files[i].Name);
                            progress?.Invoke((1.0 * i / files.Count() + h) / selected.Count, "Uploaded " + files[i].Name);
                        }
                    }
                }
            }
            catch (WebException we)
            {
                MessageBox.Show(we.Message, we.Status.ToString());
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }
Beispiel #3
0
        public static List <AzureIoTHub> GetIotHubList(
            ShowProgress progressCallback,
            ShowError errorCallback,
            RunPSCommand PSCallback)
        {
            List <AzureIoTHub> hubList = new List <AzureIoTHub>();

            try
            {
                if (MSAHelper.Subscriptions.Count == 0)
                {
                    // no subscritions means no IoT Hubs
                    return(hubList);
                }

                double progressPerSubscription = 85.0f / MSAHelper.Subscriptions.Count;
                for (int k = 0; k < MSAHelper.Subscriptions.Count; k++)
                {
                    List <Task> tasks            = new List <Task>();
                    string      subscriptionName = MSAHelper.Subscriptions[k];

                    PSCallback?.Invoke("az account set --subscription '" + subscriptionName + "'");

                    Collection <string> hubListResults = PSCallback?.Invoke("az iot hub list");
                    if (hubListResults != null && hubListResults.Count != 0)
                    {
                        for (int i = 0; i < hubListResults.Count; i++)
                        {
                            string hubName = hubListResults[i];
                            if (hubName.Contains("\"name\""))
                            {
                                hubName = hubName.Substring(hubName.IndexOf(":"));
                                hubName = hubName.Substring(hubName.IndexOf("\"") + 1);
                                hubName = hubName.Substring(0, hubName.IndexOf("\""));

                                // filter
                                if (hubName == "$fallback" || hubName == "S1" || hubName == "F1" || hubName == "B1")
                                {
                                    continue;
                                }

                                tasks.Add(Task.Run(() =>
                                {
                                    Collection <string> results2 = PSCallback("az iot hub show-connection-string --name '" + hubName + "'");
                                    if (results2 != null && results2.Count != 0)
                                    {
                                        for (int j = 0; j < results2.Count; j++)
                                        {
                                            string connectionString = results2[j];
                                            if (connectionString.Contains("\"connectionString\""))
                                            {
                                                // we have access
                                                lock (hubListLock)
                                                {
                                                    hubList.Add(new AzureIoTHub(hubName, subscriptionName));
                                                }
                                            }
                                        }
                                    }
                                }));
                            }
                        }
                    }

                    Task.WhenAll(tasks).Wait();
                    tasks.Clear();
                    progressCallback?.Invoke(progressPerSubscription, false);
                }
            }
            catch (Exception ex)
            {
                errorCallback?.Invoke(ex.Message);
            }

            return(hubList);
        }
        public static bool SignIn(
            ShowProgress progressCallback,
            ShowError errorCallback,
            RunPSCommand PSCallback)
        {
            if (CurrentState == SigninStates.SignedIn)
            {
                return(true);
            }

            try
            {
                Collection <string> results = PSCallback?.Invoke("az");
                if (results == null || results.Count == 0)
                {
                    errorCallback?.Invoke(Strings.AzureCLI);
                    if (Environment.OSVersion.Platform == PlatformID.Win32NT)
                    {
                        Process.Start(new ProcessStartInfo("https://aka.ms/installazurecliwindows"));
                    }
                    else if (Environment.OSVersion.Platform == PlatformID.Unix)
                    {
                        "sudo apt-get update".Bash();
                        "sudo apt --assume-yes install curl".Bash();
                        "curl -sL -N https://aka.ms/InstallAzureCLIDeb | sudo bash".Bash();
                    }
                    else
                    {
                        errorCallback?.Invoke(Strings.OSNotSupported);
                    }

                    return(false);
                }

                progressCallback?.Invoke(5, true);

                results = PSCallback?.Invoke("az login");
                if (results == null || results.Count == 0)
                {
                    errorCallback?.Invoke(Strings.LoginFailedAlertMessage);
                    return(false);
                }

                // enumerate subscriptions
                Subscriptions.Clear();
                for (int i = 0; i < results.Count; i++)
                {
                    string json = results[i].ToString();
                    if (json.Contains("\"name\""))
                    {
                        json = json.Substring(json.IndexOf(":"));
                        json = json.Substring(json.IndexOf("\"") + 1);
                        json = json.Substring(0, json.IndexOf("\""));
                        if (!json.Contains("@"))
                        {
                            Subscriptions.Add(json);
                        }
                    }
                }

                progressCallback?.Invoke(10, true);

                // install iot extension, if required
                PSCallback?.Invoke("az extension add --name azure-cli-iot-ext");

                progressCallback?.Invoke(15, true);

                CurrentState = SigninStates.SignedIn;

                return(true);
            }
            catch (Exception ex)
            {
                errorCallback?.Invoke(Strings.LoginFailedAlertMessage + ": " + ex.Message);
                return(false);
            }
        }