// Membre de la classe du programme // sert à la fermeture et permet de ne pas ouvrir plusieurs fois private bool m_bNetUse = false; // Au lancement du programme ou du process d'accès au répertoire distant private bool AppliqueNetUse(string sPath, string sUser, string sPass) { // Si déjà ouvert, on sort if(m_bNetUse) return; try { // simule traitement et provoque une exception si repertoire inaccessible // SI ACCESSIBLE, PAS LA PEINE D'APPLIQUER LE NET USE ! DirectoryInfo diTest = new DirectoryInfo(sPath); FileInfo[] fiTest = diTest.GetFiles(); return true; } catch ( Exception ) { try { Process p = new Process(); ProcessStartInfo pi = new ProcessStartInfo(); pi.FileName = "net"; pi.Arguments = "use " + sPath + " " + sPass + " /USER:" + sUser; pi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; p.StartInfo = pi; p.Start(); p.WaitForExit(10000); // laisse 10 seconde au net use m_bNetUse = true; } catch ( Exception netUseExc ) { //TRACE("ERREUR NET USE : " + netUseExc.Message); return false; } } } // A la fin du programme ou du process d'accès au repertoire distant private void FemeNetUse(string sPath) { if ( !m_bNetUse ) return; try { Process p = new Process(); ProcessStartInfo pi = new ProcessStartInfo(); pi.FileName = "net"; pi.Arguments = "use " + sPath + " /DELETE"; pi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; p.StartInfo = pi; p.Start(); p.WaitForExit(10000); } catch ( Exception ) { } finally{ m_bNetUse = false; } }