<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Le blog de Kropernic</title>
	<atom:link href="https://blog.developpez.com/kropernic/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.developpez.com/kropernic</link>
	<description></description>
	<lastBuildDate>Tue, 15 Jul 2014 08:43:13 +0000</lastBuildDate>
	<language>fr-FR</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.1.42</generator>
	<item>
		<title>Etablir une connexion FTPS avec .NET</title>
		<link>https://blog.developpez.com/kropernic/2014/07/15/etablir-une-connexion-ftps-avec-net/</link>
		<comments>https://blog.developpez.com/kropernic/2014/07/15/etablir-une-connexion-ftps-avec-net/#comments</comments>
		<pubDate>Tue, 15 Jul 2014 08:43:13 +0000</pubDate>
		<dc:creator><![CDATA[Kropernic]]></dc:creator>
				<category><![CDATA[VB.NET]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/kropernic/?p=99</guid>
		<description><![CDATA[De nos jours, il n&#8217;est pas rare d&#8217;échanger de l&#8217;information via le protocole FTP (File Transfert Protocol). Mais depuis l&#8217;affaire PRISM, l&#8217;accent est davantage mis sur la sécurité de ces échanges. S&#8217;il est facile de trouver des libraires permettant d&#8217;utiliser le protocole FTP et SFTP (SSH File Transfert Protocol), il l&#8217;est un peu moins d&#8217;en trouve une capable de faire du FTPS (File Transfert Protocol over SSL). Vu que je m&#8217;y suis heurté ces deux [&#8230;]]]></description>
				<content:encoded><![CDATA[<p> De nos jours, il n&rsquo;est pas rare d&rsquo;échanger de l&rsquo;information via le protocole FTP (File Transfert Protocol).  Mais depuis l&rsquo;affaire PRISM, l&rsquo;accent est davantage mis sur la sécurité de ces échanges.  S&rsquo;il est facile de trouver des libraires permettant d&rsquo;utiliser le protocole FTP et SFTP (SSH File Transfert Protocol), il l&rsquo;est un peu moins d&rsquo;en trouve une capable de faire du FTPS (File Transfert Protocol over SSL).  Vu que je m&rsquo;y suis heurté ces deux dernières semaines, je vous présentes mes &laquo;&nbsp;découvertes&nbsp;&raquo;.<br />
<span id="more-99"></span><br />
Avant d&rsquo;aller plus loin, il est bien sûr possible de faire tout cela directement avec le framework .NET (via l&rsquo;espace de nom NET) mais cela serait assez fastidieux.  Et puisque d&rsquo;autres l&rsquo;ont déjà fait pour nous, inutile de réinventer la roue.</p>
<p>Comme annoncé dans l&rsquo;introduction, je me suis heurté au problème pendant quelques temps.  Tentant d&rsquo;abord d&rsquo;utiliser directement le framework .NET., je me heurtais à une erreur me disant &laquo;&nbsp;Unable to connect to the remote server.&nbsp;&raquo;.  Sans plus de détail, c&rsquo;est un peu léger pour permettre le débogage.  J&rsquo;ai donc fouillé le net et après quelques essais avec des libraires diverses, je suis tombé sur <a href="https://netftp.codeplex.com/" title="System.Net.FtpClient" target="_blank">System.Net.FtpClient</a>.  Une libraire qui m&rsquo;a l&rsquo;air assez bien faite et simple d&rsquo;usage mais surtout, fonctionnelle !</p>
<p>Sans plus attendre, un peu de code avec, comme mise en situation, la récupération de fichiers au format CSV (pratiques pour l&rsquo;échange de données d&rsquo;une compagnie à une autre) depuis un serveur distant.</p>
<p>Premièrement (je devrais dire deuxièmement car il faut ajouter la dll aux références du projet en premier lieu), importons l&rsquo;espace de nom histoire de pouvoir coder plus légèrement.</p>
<div class="codecolorer-container vb.net mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="vbnet codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0600FF;">Imports</span> System.<span style="color: #0000FF;">Net</span>.<span style="color: #0000FF;">FtpClient</span></div></td></tr></tbody></table></div>
<p>Et maintenant le vif du sujet, la récupération des fichiers.  Voici la procédure.  Nous la commenterons ensuite ligne par ligne.</p>
<div class="codecolorer-container vb.net mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br /></div></td><td><div class="vbnet codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #FF8000;">Private</span> <span style="color: #0600FF;">Sub</span> GetCsvFiles<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">Try</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> ftps <span style="color: #008000;">=</span> <span style="color: #FF8000;">New</span> FtpClient<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ftps.<span style="color: #0000FF;">Host</span> <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;ftps.LeServeurDistant.be&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ftps.<span style="color: #0000FF;">Credentials</span> <span style="color: #008000;">=</span> <span style="color: #FF8000;">New</span> Net.<span style="color: #0000FF;">NetworkCredential</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;username&quot;</span>, <span style="color: #808080;">&quot;password&quot;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ftps.<span style="color: #0000FF;">Port</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">990</span> <span style="color: #008080; font-style: italic;">'port par defaut</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ftps.<span style="color: #0000FF;">DataConnectionType</span> <span style="color: #008000;">=</span> FtpDataConnectionType.<span style="color: #0000FF;">PASVEX</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF8000;">AddHandler</span> ftps.<span style="color: #0000FF;">ValidateCertificate</span>, <span style="color: #804040;">AddressOf</span> OnValidateCertificate<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ftps.<span style="color: #0000FF;">EncryptionMode</span> <span style="color: #008000;">=</span> FtpEncryptionMode.<span style="color: #0000FF;">Implicit</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF8000;">For</span> <span style="color: #0600FF;">Each</span> item In ftps.<span style="color: #0000FF;">GetListing</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;le/chemin/des/fichiers/&quot;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">If</span> item.<span style="color: #0000FF;">Type</span> <span style="color: #008000;">=</span> FtpFileSystemObjectType.<span style="color: #008000;">File</span> <span style="color: #FF8000;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">If</span> item.<span style="color: #0000FF;">Name</span>.<span style="color: #0000FF;">EndsWith</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;.CSV&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span style="color: #0000FF;">Writeline</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;fichier en cours : &quot;</span> <span style="color: #008000;">&amp;</span> item.<span style="color: #0000FF;">FullName</span> <span style="color: #008000;">&amp;</span> <span style="color: #808080;">&quot;)&quot;</span> <span style="color: #008080; font-style: italic;">'affichage du fichier en cours de traitement dans la console</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> reader <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">New</span> IO.<span style="color: #0000FF;">StreamReader</span><span style="color: #000000;">&#40;</span>ftps.<span style="color: #0000FF;">OpenRead</span><span style="color: #000000;">&#40;</span>item.<span style="color: #0000FF;">FullName</span><span style="color: #000000;">&#41;</span>, Text.<span style="color: #0000FF;">Encoding</span>.<span style="color: #0600FF;">Default</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> writer <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">New</span> IO.<span style="color: #0000FF;">StreamWriter</span><span style="color: #000000;">&#40;</span>My.<span style="color: #0000FF;">Settings</span>.<span style="color: #0000FF;">storageFolder</span> <span style="color: #008000;">&amp;</span> item.<span style="color: #0000FF;">Name</span>, <span style="color: #0600FF;">False</span>, Text.<span style="color: #0000FF;">Encoding</span>.<span style="color: #0600FF;">Default</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> <span style="color: #0600FF;">line</span> <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> <span style="color: #008000;">=</span> reader.<span style="color: #0000FF;">ReadLine</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">While</span> <span style="color: #804040;">Not</span> reader.<span style="color: #0000FF;">EndOfStream</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">line</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">line</span> <span style="color: #008000;">=</span> reader.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">While</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">line</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0600FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.<span style="color: #0000FF;">Dispose</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reader.<span style="color: #0600FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; reader.<span style="color: #0000FF;">Dispose</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF8000;">Next</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">If</span> ftps.<span style="color: #0000FF;">IsConnected</span> <span style="color: #FF8000;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ftps.<span style="color: #0000FF;">Disconnect</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">Catch</span> ex <span style="color: #FF8000;">As</span> Exception<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>ex.<span style="color: #0000FF;">Message</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Try</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span></div></td></tr></tbody></table></div>
<p>Comme vous l&rsquo;aurez remarqué, j&rsquo;ai choisi d&rsquo;utiliser un projet de type Application Console.  En effet, pas besoin ici de jolis écrans.  La console nous suffira pour afficher les infos utiles à suivre le bon déroulement du processus.</p>
<p>Décortiquons à présent ensemble cette procédure.</p>
<ul>
<li>Ligne 2 : Ouverture d&rsquo;un bloc Try&#8230;Catch.  Une bonne habitude à prendre, tout simplement.</li>
<li>Lignes 3-6 : Création d&rsquo;un objet FtpClient (que j&rsquo;ai nommé ftps) auquel on assigne les valeurs qui vont bien aux propriétés habituelles que sont Host, Port et Credentials. </li>
<li>Ligne 7 : Le type de connexion.  Vous avez ici plusieurs possibilités.  Dans mon cas, le serveur distant me renvoyant une adresse privée (192.168.X.X), il me fallait utilisé PASVEX.</li>
<li>Ligne 9 : Ajout d&rsquo;un gestionnaire d&rsquo;événement pour la validation des certificats.  J&rsquo;y reviendrai après.</li>
<li>Ligne 11 : Il s&rsquo;agit ici de la raison pour laquelle nous faisons tout ça.  C&rsquo;est cette ligne qui définit le mode d&rsquo;encryption des données.  Avec le mode Implicit, les données sont cryptées de bout en bout.  Avec le mode Explicit, l&rsquo;encryption démarre après les politesses d&rsquo;usages échangées avec le serveur.</li>
<li>Ligne 13 : Démarrage d&rsquo;une boucle qui va parcourir les éléments du répertoire (du serveur distant) passé en paramètre.</li>
<li>Ligne 14 : Vérification que l&rsquo;élément en cours est bien un fichier.  Il pourrait s&rsquo;agir d&rsquo;un répertoire ou d&rsquo;un lien symbolique.</li>
<li>Lignes 15-30 : Ici, rien de bien spécifique.  Si le fichier est bien un fichier CSV, on crée un stream de lecture et un stream d&rsquo;écriture et on copie le fichier ligne par ligne en n&rsquo;oubliant pas de libérer les ressources à la fin.  Pour un autre format de fichier, cette méthode pourrait ne pas convenir.  Notez également que j&rsquo;utilise les paramètres de projet pour définir où stocker le fichier de notre côté.</li>
<li>Lignes 32-34 : Si ce n&rsquo;est pas déjà fait, fermeture de la connexion qui a été ouverte par la commande GetListing.  On n&rsquo;est jamais trop prudent.</li>
<li>Lignes 35-37 : Affichage du message d&rsquo;erreur éventuel.
</ul>
<p>Voilà pour ce qui est du code de la procédure.  Revenons maintenant sur la validation des certificats.  Car qui dit FTPS, dit certificats à accepter par celui qui se connecte au serveur distant (c&rsquo;est-à-dire nous).<br />
Dans mon cas, je n&rsquo;ai pas fait la fine bouche.  La récupération de ces données étant primordiales et la source digne de confiance, je les accepte quoi qu&rsquo;il arrive.  Voici ce que cela donne :</p>
<div class="codecolorer-container vb.net mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="vbnet codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #FF8000;">Private</span> <span style="color: #0600FF;">Sub</span> OnValidateCertificate<span style="color: #000000;">&#40;</span>control <span style="color: #FF8000;">As</span> FtpClient, e <span style="color: #FF8000;">As</span> FtpSslValidationEventArgs<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; e.<span style="color: #0000FF;">Accept</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">True</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span></div></td></tr></tbody></table></div>
<p>Pour les plus pointilleux, les objets X509Certificates, 509Chain et SslPolicyErrors sont accessibles via la variable d&rsquo;événement e.  Libre à vous de gérer la validation des certificats comme bon vous semble.  Personnellement, ce n&rsquo;est pas ma tasse de thé et de loin.</p>
<p>Et voilà, il ne reste plus qu&rsquo;à appeler cette procédure dans le main de notre projet et le tour est joué.</p>
<p>En espérant que cela en aidera plus d&rsquo;un.</p>
<p>Kropernic.</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Gérer l&#8217;événement KeyPress d&#8217;une cellule d&#8217;un DataGridView</title>
		<link>https://blog.developpez.com/kropernic/2014/06/27/gerer-levenement-keypress-dune-cellule-dun-datagridview/</link>
		<comments>https://blog.developpez.com/kropernic/2014/06/27/gerer-levenement-keypress-dune-cellule-dun-datagridview/#comments</comments>
		<pubDate>Fri, 27 Jun 2014 09:54:26 +0000</pubDate>
		<dc:creator><![CDATA[Kropernic]]></dc:creator>
				<category><![CDATA[VB.NET]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/kropernic/?p=87</guid>
		<description><![CDATA[Gérer l’événement d&#8217;un contrôle est une chose basique à laquelle chaque débutant c&#8217;est un jour heurté. Une fois que le mécanisme est compris, la chose est en fait assez facile. Mais la chose est un peu moins triviale lorsqu&#8217;il s&#8217;agissait d&#8217;une cellule d&#8217;un DataGridView. En effet, le contrôle DataGridView ne possède pas d&#8217;événement CellKeyPress vers lequel on serait tenté de se tourner (je n&#8217;entrerai pas dans les détails ici mais c&#8217;est en fait tout à [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Gérer l’événement d&rsquo;un contrôle est une chose basique à laquelle chaque débutant c&rsquo;est un jour heurté.  Une fois que le mécanisme est compris, la chose est en fait assez facile.  Mais la chose est un peu moins triviale lorsqu&rsquo;il s&rsquo;agissait d&rsquo;une cellule d&rsquo;un DataGridView.<br />
<span id="more-87"></span><br />
En effet, le contrôle DataGridView ne possède pas d&rsquo;événement CellKeyPress vers lequel on serait tenté de se tourner (je n&rsquo;entrerai pas dans les détails ici mais c&rsquo;est en fait tout à fait normal que cet événement ne soit pas présent).  Lorsque l&rsquo;utilisateur édite une cellule, il n&rsquo;édite en fait pas directement dans le DataGridView.  En effet, ce dernier crée et affiche un contrôle en avant plan de la cellule pour permettre l&rsquo;édition.  Et c&rsquo;est sûr ce contrôle qu&rsquo;il faut gérer l&rsquo;événement qui nous intéresse.</p>
<p>Voyons comment cela fonctionne concrètement.</p>
<p>Pour rester concis, disons que nous voulons empêcher toutes autres saisies qu&rsquo;un nombre entier dans la cellule.</p>
<p>Commençons par écrire la procédure pour gérer la pression de touche proprement dite :</p>
<div class="codecolorer-container vb.net mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="vbnet codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #FF8000;">Private</span> <span style="color: #0600FF;">Sub</span> EditingControl_KeyPress<span style="color: #000000;">&#40;</span>sender <span style="color: #FF8000;">As</span> System.<span style="color: #FF0000;">Object</span>, e <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">Windows</span>.<span style="color: #0000FF;">Forms</span>.<span style="color: #0000FF;">KeyPressEventArgs</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">If</span> <span style="color: #804040;">Not</span> Char.<span style="color: #0000FF;">IsNumber</span><span style="color: #000000;">&#40;</span>e.<span style="color: #0000FF;">KeyChar</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.<span style="color: #0000FF;">Handled</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">True</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span></div></td></tr></tbody></table></div>
<p>Cela se passe d&rsquo;explication je pense.</p>
<p>L&rsquo;astuce consiste maintenant à savoir comment ajouter la gestion de l&rsquo;événement sur le contrôle d&rsquo;édition afficher par notre DataGridView.  C&rsquo;est en fait assez simple mais il fallait le savoir. Ce contrôle nous est accessible très facilement via l&rsquo;événement EditingControlShowing du DataGridView.</p>
<p>Nous pouvons donc maintenant écrire quelque chose dans ce goût-là :</p>
<div class="codecolorer-container vb.net mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="vbnet codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #FF8000;">Private</span> editingControl <span style="color: #FF8000;">As</span> Control<br />
<br />
&nbsp; &nbsp; <span style="color: #FF8000;">Private</span> <span style="color: #0600FF;">Sub</span> dgvExample_EditingControlShowing<span style="color: #000000;">&#40;</span>sender <span style="color: #FF8000;">As</span> System.<span style="color: #FF0000;">Object</span>, e <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">Windows</span>.<span style="color: #0000FF;">Forms</span>.<span style="color: #0000FF;">DataGridViewEditingControlShowingEventArgs</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">Handles</span> dgvExample.<span style="color: #0000FF;">EditingControlShowing</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">If</span> dgvExample.<span style="color: #0000FF;">CurrentCell</span>.<span style="color: #0000FF;">OwningColumn</span>.<span style="color: #0000FF;">Name</span> <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;Le nom de la colonne en question&quot;</span> <span style="color: #FF8000;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; editingControl <span style="color: #008000;">=</span> e.<span style="color: #0000FF;">Control</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF8000;">AddHandler</span> editingControl.<span style="color: #0000FF;">KeyPress</span>, <span style="color: #804040;">AddressOf</span> EditingControl_KeyPress<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span></div></td></tr></tbody></table></div>
<p>Ici, nous vérifions donc que la colonne que l&rsquo;utilisateur édite est bien celle qui nous intéresse en vérifiant son nom et si le test est positif, nous nous abonnons donc à l&rsquo;événement KeyPress de ce contrôle accessible via le paramètre e de l&rsquo;événement.  Et nous conservons une référence vers ce contrôle via la variable editingControl.</p>
<p>Il ne reste plus qu&rsquo;à nous désabonner à l&rsquo;événement contrôle une fois que l&rsquo;édition est finie.  Et c&rsquo;est pour cela qu&rsquo;il nous fallait garder une référence vers le contrôle en question.  Car là, le framework ne nous donne pas &laquo;&nbsp;d&rsquo;accès simplifié&nbsp;&raquo;.</p>
<div class="codecolorer-container vb.net mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="vbnet codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #FF8000;">Private</span> <span style="color: #0600FF;">Sub</span> dgvExample_CellEndEdit<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> sender <span style="color: #FF8000;">As</span> System.<span style="color: #FF0000;">Object</span>, <span style="color: #FF8000;">ByVal</span> e <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">Windows</span>.<span style="color: #0000FF;">Forms</span>.<span style="color: #0000FF;">DataGridViewCellEventArgs</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">Handles</span> dgvExample.<span style="color: #0000FF;">CellEndEdit</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">If</span> dgvExample.<span style="color: #0000FF;">CurrentCell</span>.<span style="color: #0000FF;">OwningColumn</span>.<span style="color: #0000FF;">Name</span> <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;Le nom de la colonne en question&quot;</span> <span style="color: #FF8000;">Then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; RemoveHandler editingControl.<span style="color: #0000FF;">KeyPress</span>, <span style="color: #804040;">AddressOf</span> EditingControl_KeyPress<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span><br />
&nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span></div></td></tr></tbody></table></div>
<p>Et voilà, nous pouvons à présent empêcher les utilisateurs d&rsquo;encoder n&rsquo;importe quoi.</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>T-SQL : Récupérer le numéro de semaine d&#8217;une date</title>
		<link>https://blog.developpez.com/kropernic/2013/09/25/t-sql-recuperer-le-numero-de-semaine-dune-date/</link>
		<comments>https://blog.developpez.com/kropernic/2013/09/25/t-sql-recuperer-le-numero-de-semaine-dune-date/#comments</comments>
		<pubDate>Wed, 25 Sep 2013 13:23:26 +0000</pubDate>
		<dc:creator><![CDATA[Kropernic]]></dc:creator>
				<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/kropernic/?p=66</guid>
		<description><![CDATA[Trouver le numéro de la semaine d&#8217;une date donnée est un besoin qui revient régulièrement lorsque l&#8217;on en vient à gérer des données temporelles. Si au premier abord cela semble trivial, la fonction DATEPART pourrait bien vous réserver quelques surprises si vous n&#8217;y prenez pas garde (comme cela a été mon cas aujourd&#8217;hui). Il ne s&#8217;agit pas ici de faire un descriptif détaillé de la fonction DATEPART. Pour cela, il y a la MSDN. Intéressons-nous [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Trouver le numéro de la semaine d&rsquo;une date donnée est un besoin qui revient régulièrement lorsque l&rsquo;on en vient à gérer des données temporelles.  Si au premier abord cela semble trivial, la fonction DATEPART pourrait bien vous réserver quelques surprises si vous n&rsquo;y prenez pas garde (comme cela a été mon cas aujourd&rsquo;hui).<br />
<span id="more-66"></span><br />
Il ne s&rsquo;agit pas ici de faire un descriptif détaillé de la fonction DATEPART.  Pour cela, il y a la <a href="http://msdn.microsoft.com/en-us/library/ms174420.aspx" title="MSDN" target="_blank">MSDN</a>.</p>
<p>Intéressons-nous donc à l&rsquo;argument WEEK.  Comme on peut s&rsquo;y attendre, l&rsquo;utilisation de cette argument nous renvoi un numéro de semaine.  Mais déjà un piège pointe le bout de son nez.<br />
Selon la MSDN :</p>
<blockquote><p>When datepart is week (wk, ww) or weekday (dw), the return value depends on the value that is set by using SET DATEFIRST. </p></blockquote>
<p>Traduction :</p>
<blockquote><p>Lorsque le paramètre est week (wk, ww) ou weekday (dw), le résultat dépend de la valeur définie par l&rsquo;instruction SET DATEFIRST.</p></blockquote>
<p>En effet, SET DATEFIRST permet de signaler à SQL SERVER quel est le premier jour d&rsquo;une semaine.</p>
<p>En pratique, qu&rsquo;est-ce que cela signifie ?</p>
<p>Voyons un cas concret.  Pour nous faciliter la vie, créons une table de dates toute simple dont voici le code DDL ainsi que le code d&rsquo;insertion de quelques lignes :</p>
<div class="codecolorer-container sql mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br /></div></td><td><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> T_DATE_DAT<span style="color: #66cc66;">&#40;</span><br />
&nbsp; &nbsp; DAT_DATE <span style="color: #993333; font-weight: bold;">DATE</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span><br />
<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #993333; font-weight: bold;">GO</span><br />
<br />
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> T_DATE_DAT<br />
&nbsp; &nbsp; <span style="color: #993333; font-weight: bold;">VALUES</span>&nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'20120101'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'20120102'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'20120103'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'20120104'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'20120105'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'20120106'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'20120107'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'20120108'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'20120109'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'20120110'</span><span style="color: #66cc66;">&#41;</span></div></td></tr></tbody></table></div>
<p>Sur base de ceci, nous pouvons maintenant faire quelques tests.  Par défaut, le premier jour d&rsquo;une semaine pour SQL SERVER est le dimanche.<br />
Exécutons alors la requête suivante :</p>
<div class="codecolorer-container sql mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; DAT_DATE<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; DATEPART<span style="color: #66cc66;">&#40;</span>WEEK<span style="color: #66cc66;">,</span>DAT_DATE<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">'WEEK'</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; T_DATE_DAT</div></td></tr></tbody></table></div>
<p>Vous devriez obtenir comme résultat que la première semaine de 2012 va du 1 Janvier au 7 Janvier.  Le premier étant un dimanche et le 7 un samedi.  </p>
<p>Ce n&rsquo;est pas vraiment ce nous attentions n&rsquo;est-ce pas ?</p>
<p>Signalons maintenant à SQL SERVER que le premier jour d&rsquo;une semaine est le lundi et exécutons à nouveau cette requête.</p>
<div class="codecolorer-container sql mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br /></div></td><td><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SET</span> DATEFIRST <span style="color: #cc66cc;">1</span>;<br />
<br />
<span style="color: #993333; font-weight: bold;">SELECT</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; DAT_DATE<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; DATEPART<span style="color: #66cc66;">&#40;</span>WEEK<span style="color: #66cc66;">,</span>DAT_DATE<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">'WEEK'</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; T_DATE_DAT</div></td></tr></tbody></table></div>
<p>A présent, nos semaines commencent bien le lundi.  C&rsquo;est déjà un net progrès !  Cependant, si je regarde un calendrier sur lequel sont notés les numéros des semaines, je constate que la première semaine de 2012 commence en fait le 2 Janvier !  Cela s&rsquo;explique par le fait que la première semaine d&rsquo;une année est la semaine comprenant le premier jeudi de la-dite année.  La première semaine de 2012 est donc bien la semaine allant du 2 Janvier au 8 Janvier.<br />
N.B. : Cette numérotation n&rsquo;est principalement utilisée qu&rsquo;en Europe.</p>
<p>Et c&rsquo;est là qu&rsquo;est le piège.  S&rsquo;il est normal d&rsquo;avoir des années de 53 semaines, l&rsquo;utilisation de la fonction DATEPART avec l&rsquo;argument WEEK vous retourna des semaines avec un numéro 54.</p>
<p>Mais alors comment faire pour récupérer le bon numéro de semaine avec la fonction DATEPART.  Et bien tout simplement en n&rsquo;utilisant pas l&rsquo;argument WEEK mais ISOWK.  Et oui, il fallait le savoir, il y a un argument pour récupérer le numéro de la semaine au format ISO.</p>
<p>Remplaçons donc le paramètre WEEK par ISOWK dans notre requête.  Cela donne donc la requête suivante :</p>
<div class="codecolorer-container sql mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="sql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #993333; font-weight: bold;">SELECT</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; DAT_DATE<span style="color: #66cc66;">,</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; DATEPART<span style="color: #66cc66;">&#40;</span>ISOWK<span style="color: #66cc66;">,</span>DAT_DATE<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #ff0000;">'ISO WEEK'</span><br />
<span style="color: #993333; font-weight: bold;">FROM</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; T_DATE_DAT</div></td></tr></tbody></table></div>
<p>Et nous récupérons maintenant les numéros de semaines correctes tels que nous les retrouvons sur tous calendriers (européens) qui se respecte.</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Utilisation d&#8217;un fichier de configuration pour une dll</title>
		<link>https://blog.developpez.com/kropernic/2013/06/18/utilisation-dun-fichier-de-configuration-pour-une-dll/</link>
		<comments>https://blog.developpez.com/kropernic/2013/06/18/utilisation-dun-fichier-de-configuration-pour-une-dll/#comments</comments>
		<pubDate>Tue, 18 Jun 2013 13:20:01 +0000</pubDate>
		<dc:creator><![CDATA[Kropernic]]></dc:creator>
				<category><![CDATA[VB.NET]]></category>
		<category><![CDATA[app.config]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[dll]]></category>

		<guid isPermaLink="false">http://blog.developpez.com/kropernic/?p=10</guid>
		<description><![CDATA[Bonjour, Pour ceux qui comme moi voudraient utiliser un fichier de configuration avec une dll pour, par exemple, stocker une chaîne de connexion à une base de données, voici la procédure à suivre. Premièrement, il faut bien sûr ajouter un fichier de configuration au projet et le nommer &#171;&#160;&#60;nom_du_projet&#62;.dll.config&#160;&#187;. Votre fichier devrait ressembler à ceci : Deuxièmement, il faut modifier les propriétés du fichier pour qu&#8217;il se copie dans le répertoire d&#8217;output du projet GUI. [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>Bonjour,</p>
<p>Pour ceux qui comme moi voudraient utiliser un fichier de configuration avec une dll pour, par exemple, stocker une chaîne de connexion à une base de données, voici la procédure à suivre.<br />
<span id="more-10"></span><br />
Premièrement, il faut bien sûr ajouter un fichier de configuration au projet et le nommer &laquo;&nbsp;&lt;nom_du_projet&gt;.dll.config&nbsp;&raquo;.<br />
Votre fichier devrait ressembler à ceci :<br />
<a href="http://blog.developpez.com/kropernic/files/2013/06/app-config.png"><img src="http://blog.developpez.com/kropernic/files/2013/06/app-config-300x87.png" alt="app-config" width="300" height="150" class="alignnone size-medium wp-image-19" /></a></p>
<p>Deuxièmement, il faut modifier les propriétés du fichier pour qu&rsquo;il se copie dans le répertoire d&rsquo;output du projet GUI.<br />
<a href="http://blog.developpez.com/kropernic/files/2013/06/properties.png"><img src="http://blog.developpez.com/kropernic/files/2013/06/properties-300x54.png" alt="properties" width="300" height="54" class="alignnone size-medium wp-image-59" /></a><br />
N.B. : Il est à noté que C# et VB ne gère pas les solutions multi-projets de la même manière.  Il sera donc peut-être nécessaire de le copier manuellement.</p>
<p>Il faut maintenant ajouter la partie permettant de stocker la chaîne de connexion.<a href="http://blog.developpez.com/kropernic/files/2013/06/appSettings.png"><img src="http://blog.developpez.com/kropernic/files/2013/06/appSettings-300x98.png" alt="appSettings" width="300" height="98" class="alignnone size-medium wp-image-21" /></a></p>
<p>Jusque-là, rien de bien sorcier.  </p>
<p>And now, the tricky part ! (Et maintenant, la partie la plus délicate !)</p>
<p>Le problème est qu&rsquo;il n&rsquo;est pas possible d&rsquo;accéder aux informations placées dans ce fichier de configuration via <strong>My.Settings</strong>.<br />
Il faut donc ruser pour ouvrir ce fichier de configuration et accéder à la partie qui nous intéresse (car il pourrait contenir bien d&rsquo;autres choses que juste la chaîne de connexion).</p>
<p>Voici le code de la fonction que j&rsquo;utilise dans la classe de base de ma couche DAL.</p>
<div class="codecolorer-container vb.net mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="vbnet codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #FF8000;">Private</span> <span style="color: #FF8000;">Shared</span> <span style="color: #0600FF;">Function</span> CreateConnection<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">As</span> SqlConnection<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> cnx <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">New</span> SqlConnection<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">'Open the configuration file using the dll location</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> myDllConfig <span style="color: #FF8000;">As</span> Configuration <span style="color: #008000;">=</span> ConfigurationManager.<span style="color: #0000FF;">OpenExeConfiguration</span><span style="color: #000000;">&#40;</span><span style="color: #FF8000;">Assembly</span>.<span style="color: #0000FF;">GetExecutingAssembly</span>.<span style="color: #0000FF;">Location</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">' Get the appSettings section</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">Dim</span> myDllConfigAppSettings <span style="color: #FF8000;">As</span> AppSettingsSection <span style="color: #008000;">=</span> DirectCast<span style="color: #000000;">&#40;</span>myDllConfig.<span style="color: #0000FF;">GetSection</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;appSettings&quot;</span><span style="color: #000000;">&#41;</span>, AppSettingsSection<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">' return the desired field </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; cnx.<span style="color: #0000FF;">ConnectionString</span> <span style="color: #008000;">=</span> myDllConfigAppSettings.<span style="color: #0000FF;">Settings</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;MyConnectionString&quot;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Value</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #FF8000;">Return</span> cnx<br />
&nbsp; &nbsp; <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Function</span></div></td></tr></tbody></table></div>
<p>En quelque mot, voici ce qu&rsquo;elle fait : </p>
<ul>
<li>instanciation de la connexion</li>
<li>ouverture du fichier de configuration</li>
<li>récupération de la bonne partie du fichier</li>
<li>affectation de la chaine de connexion</li>
<li>renvoi de la connexion</li>
</ul>
<p>Vous voilà maintenant capable d&rsquo;utiliser un fichier de configuration avec une dll.</p>
]]></content:encoded>
			<wfw:commentRss></wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
