Skip to content

Instantly share code, notes, and snippets.

@DarthJahus
Created July 11, 2016 00:47
Show Gist options
  • Save DarthJahus/b69225d6b932aea6df45cc8cfe23c7d1 to your computer and use it in GitHub Desktop.
Save DarthJahus/b69225d6b932aea6df45cc8cfe23c7d1 to your computer and use it in GitHub Desktop.
// Jahus, 2016-07-11 01:42 GMT+1
// Fonction qui renvoie un dictionnaire composé des paramètres et de leurs valeurs
public Dictionary<string, string> GetURIParams(string strURI)
{
Regex rx = new Regex("[?|&]([^=]+)=([^&]+)"); // Cherche tous les paramètres dans une URI
MatchCollection rxm = rx.Matches(strURI); // Effectue la recherche dans l'URI "MyInput"
Dictionary<string, string> Params = new Dictionary<string, string>(); // Crée un dictionnaire des paramètres
foreach (Match rxm_match in rxm) // Parcours les Matches (unité : Match)
{
string MatchValue = rxm_match.Groups[0].Value; // group 0 contient la chaîne trouvée complète
string ParamName = rxm_match.Groups[1].Value; // group 1 contient $1
string ParamValue = rxm_match.Groups[2].Value; // group 2 contient $2
Params.Add(ParamName, ParamValue); // Ajoute le paramètre au dictionnaire
}
return Params;
}
// Utilisation dans le cas des commentaires du HUB
string MyURI = "feedback-hub:?feedbackid=e9d7481c-bd84-4439-8c51-33bca8d29f4a&contextid=274&form=2&src=1";
Dictionnary<string, string> Params = GetURIParams(MyURI);
if (Params.ContainsKey("feedbackid") && Params.ContainsKey("contextid"))
{
// Les paramètres sont présents
// Prendre Params["feedbackid"] et Params["contextid"]
}
else
{
// Les paramètres ne sont pas présents
}
// Utilisation dans le cas général
private void button1_Click(object sender, EventArgs e)
{
string MyURI = textBox1.Text; // Une boite de texte, quoi !
Dictionary<string, string> Params = GetURIParams(MyURI);
if (Params.Count != 0)
{
// Les paramètres sont présents
// Prendre Params["feedbackid"] et Params["contextid"]
label1.Text = ""; // Un Label pour accueillir le résultat
foreach (string Key in Params.Keys)
{
label1.Text += String.Format("{0} = {1}\n", Key, Params[Key]);
}
}
else
{
// Les paramètres ne sont pas présents
label1.Text = "No param found!";
}
}
// EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment