1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
Imports System.IO
Imports System.Xml
Imports System.Text
Imports System.Security.Cryptography
Public Class Form1
Sub associerExtension()
Dim extension As String = ".osms"
Dim nomSansPoint As String = extension.Substring(1, extension.Length - 1) 'on aura uniquement test1
Dim cheminFinalProgramme As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
My.Computer.Registry.ClassesRoot.CreateSubKey(extension).SetValue("", nomSansPoint, Microsoft.Win32.RegistryValueKind.String)
My.Computer.Registry.ClassesRoot.CreateSubKey(nomSansPoint & "\shell\open\command").SetValue _
("", cheminFinalProgramme & " ""%l"" ", Microsoft.Win32.RegistryValueKind.String)
End Sub
Public Sub LireXML(ByVal XML As String)
Dim documents As New XmlDocument
documents.LoadXml(XML)
Dim prenom As XmlNodeList = documents.GetElementsByTagName("Prenom")
Dim nom As XmlNodeList = documents.GetElementsByTagName("Nom")
'On va parcourir tous les élements Prénoms, ici un seul
For i As Integer = 0 To prenom.Count = 1
SouhaiterBienvenue(prenom(i).InnerText, nom(i).InnerText)
Next
End Sub
Public Sub SouhaiterBienvenue(ByVal prenom As String, ByVal nom As String)
Dim utilis As String = String.Format("{0} {1}", prenom, nom)
Label1.Text = utilis 'on souhaite la bienvenue à l'utilisateur ayant enregistré le fichier test1
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try 'on gère les erreurs
If My.Computer.Registry.ClassesRoot.OpenSubKey(".osms") Is Nothing Then
'si l'extension n'est pas encore associée, on l'associe.
associerExtension()
End If
Catch ex As Exception
MsgBox("Erreur !") 'message très descriptif..
End Try
'on obtient les arguments, à savoir le nom du fichier avec lequel le prog s'est ouvert
For Each argument As String In Environment.GetCommandLineArgs()
If argument.Contains(".osms") Then 'si il contient notre extension
TextBox3.Text = TextBox3.Text & argument 'on affiche le chemin du fichier test1
LireXML(Decryptage(My.Computer.FileSystem.ReadAllText(argument)))
End If
Next
End Sub
Sub enregistrerPref()
Dim donneesXML As String = "<?xml version=""1.0"" encoding=""utf-8"" ?>" 'ligne nécessaire pour adhérer au standard
donneesXML = String.Concat(donneesXML, vbNewLine, "<Enregistrement>") 'notre balise principale
donneesXML = String.Concat(donneesXML, vbNewLine, vbTab, "<Prenom>" & TextBox2.Text & "</Prenom>")
donneesXML = String.Concat(donneesXML, vbNewLine, vbTab, "<Nom>" & TextBox1.Text & "</Nom>")
donneesXML = String.Concat(donneesXML, vbNewLine, "</Enregistrement>")
'on enregistre sous notre propre extension (et ici, sur le bureau)
Using sr As New StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) & "\monEx.osms")
sr.Write(Cryptage(donneesXML))
End Using
End Sub
Public Function Cryptage(ByVal donnees As String) As String
Dim sha1 As New SHA1Managed
Convert.ToBase64String(sha1.ComputeHash(Encoding.ASCII.GetBytes(donnees)))
Dim donneeCryptees() As Byte = ASCIIEncoding.ASCII.GetBytes(donnees)
Dim cryptageEnString As String = Convert.ToBase64String(donneeCryptees)
Return cryptageEnString
End Function
Public Function Decryptage(ByVal Data As String) As String
Dim dd() As Byte = Convert.FromBase64String(Data)
Dim decryptageEnString As String = Encoding.ASCII.GetString(dd)
Return decryptageEnString
End Function
Sub ajouterIcone()
My.Computer.Registry.ClassesRoot.OpenSubKey(".osms").CreateSubKey("DefautIcon").SetValue("", ",0")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
enregistrerPref()
End Sub
End Class |
Partager