Guest
Public paste!

Untitled

By: a guest | Mar 22nd, 2010 | Syntax: C# | Size: 11.46 KB | Hits: 57 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using aejw.Network;
  10. using Credentials;
  11.  
  12. namespace Media
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.  
  17.         NetworkDrive oNetDrive = new aejw.Network.NetworkDrive();
  18.         string[] aUser = { "Brendan", "Media", "BT-Share", "BT-LAN" };
  19.         string[] aPass = { "password1", "password2", "password3", "password4" };
  20.         string[] aClearance = { "Level 0", "Level 1", "Level 2", "Level 3" };
  21.  
  22.         public Form1()
  23.         {
  24.             InitializeComponent();
  25.         }
  26.  
  27.         private void button1_Click(object sender, EventArgs e)
  28.         {
  29.            MediaI brendon = new MediaI("c", "share", "brendon", "password");
  30.            
  31.         }
  32.  
  33.         private void Form1_Load(object sender, EventArgs e)
  34.         {
  35.  
  36.         }
  37.  
  38.         private void label1_Click(object sender, EventArgs e)
  39.         {
  40.  
  41.         }
  42.  
  43.         private void L1_Click(object sender, EventArgs e)
  44.         {
  45.             Login login = new Login();
  46.         }
  47.     }
  48. }
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58. using System;
  59. using System.Collections.Generic;
  60. using System.Linq;
  61. using System.Text;
  62. using aejw.Network;
  63.  
  64. namespace Credentials
  65. {
  66.  
  67.     class MediaI
  68.     {
  69.         NetworkDrive oNetDrive = new aejw.Network.NetworkDrive();
  70.         string[] aUser = { "Brendan", "Media", "BT-Share", "BT-LAN" };
  71.         string[] aPass = { "password1", "password2", "password3", "password4" };
  72.         string[] aClearance = { "Level 0", "Level 1", "Level 2", "Level 3" };
  73.  
  74.         public MediaI(string localDrive, string shareName, string user, string password)
  75.         {
  76.             oNetDrive.LocalDrive = localDrive;
  77.             oNetDrive.ShareName = shareName;
  78.             oNetDrive.MapDrive(user, password);
  79.         }
  80.  
  81.         public void Brendan()
  82.         {
  83.             oNetDrive.LocalDrive = "M:";
  84.             oNetDrive.ShareName = "\\\\BT-Main\\Media I";
  85.             oNetDrive.MapDrive(aUser[0], aPass[0]);
  86.         }
  87.  
  88.         public void Georgia()
  89.         {
  90.             oNetDrive.LocalDrive = "M:";
  91.             oNetDrive.ShareName = "\\\\BT-Main\\Media I";
  92.             oNetDrive.MapDrive(aUser[1], aPass[1]);
  93.         }
  94.  
  95.         public void Maura()
  96.         {
  97.             oNetDrive.LocalDrive = "M:";
  98.             oNetDrive.ShareName = "\\\\BT-Main\\Media I";
  99.             oNetDrive.MapDrive(aUser[1], aPass[1]);
  100.         }
  101.  
  102.         public void Brett()
  103.         {
  104.             oNetDrive.LocalDrive = "M:";
  105.             oNetDrive.ShareName = "\\\\BT-Main\\Media I";
  106.             oNetDrive.MapDrive(aUser[1], aPass[1]);
  107.         }
  108.  
  109.         public void LAN()
  110.         {
  111.             oNetDrive.LocalDrive = "M:";
  112.             oNetDrive.ShareName = "\\\\BT-Main\\Media I";
  113.             oNetDrive.MapDrive(aUser[3], aPass[3]);
  114.         }
  115.     }
  116.  
  117.     class MediaII
  118.     {
  119.         NetworkDrive oNetDrive = new aejw.Network.NetworkDrive();
  120.         string[] aUser = { "Brendan", "Media", "BT-Share", "BT-LAN" };
  121.         string[] aPass = { "password1", "password2", "password3", "password4" };
  122.         string[] aClearance = { "Level 0", "Level 1", "Level 2", "Level 3" };
  123.  
  124.         public void Brendan()
  125.         {
  126.             oNetDrive.LocalDrive = "M:";
  127.             oNetDrive.ShareName = "\\\\BT-Main\\Media I";
  128.             oNetDrive.MapDrive(aUser[0], aPass[0]);
  129.         }
  130.     }
  131.    
  132. }
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148. /*==============================================================================================================
  149.  
  150.   [ cNetworkDrive - Network Drive API Class ]
  151.   -------------------------------------------
  152.   Copyright (c)2006 aejw.com
  153.   http://www.aejw.com/
  154.  
  155. Build:         0017 - May 2006
  156. Thanks To:     'jsantos98' from CodeProject.com for his update allowing the local / drive not to specifyed
  157.  
  158. EULA:          Creative Commons - Attribution-ShareAlike 2.5
  159.                http://creativecommons.org/licenses/by-sa/2.5/
  160.  
  161. Disclaimer:    THIS FILES / SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS"
  162.                AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  163.                TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  164.                PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  165.                OWNER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  166.                EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  167.                TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  168.                OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  169.                THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  170.                TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  171.                OF THE USE OF THIS FILES / SOFTWARE, EVEN IF ADVISED OF THE
  172.                POSSIBILITY OF SUCH DAMAGE. USE AT YOUR OWN RISK.
  173.                            
  174. ==============================================================================================================*/
  175. using System;
  176. using System.Windows.Forms;
  177. using System.Runtime.InteropServices;
  178.  
  179. namespace aejw.Network
  180. {
  181.         /// <summary>
  182.         /// Network Drive Interface
  183.         /// </summary>
  184.         public class NetworkDrive
  185.         {
  186.  
  187.                 #region API
  188.                 [DllImport("mpr.dll")] private static extern int WNetAddConnection2A(ref structNetResource pstNetRes, string psPassword, string psUsername, int piFlags);
  189.                 [DllImport("mpr.dll")] private static extern int WNetCancelConnection2A(string psName, int piFlags, int pfForce);
  190.                 [DllImport("mpr.dll")] private static extern int WNetConnectionDialog(int phWnd, int piType);
  191.                 [DllImport("mpr.dll")] private static extern int WNetDisconnectDialog(int phWnd, int piType);
  192.                 [DllImport("mpr.dll")] private static extern int WNetRestoreConnectionW(int phWnd, string psLocalDrive);
  193.  
  194.                 [StructLayout(LayoutKind.Sequential)]
  195.                 private struct structNetResource{
  196.                         public int iScope;
  197.                         public int iType;
  198.                         public int iDisplayType;
  199.                         public int iUsage;
  200.                         public string sLocalName;
  201.                         public string sRemoteName;
  202.                         public string sComment;
  203.                         public string sProvider;
  204.                 }
  205.                
  206.                 private const int RESOURCETYPE_DISK = 0x1;
  207.                
  208.                 //Standard     
  209.                 private const int CONNECT_INTERACTIVE = 0x00000008;
  210.                 private const int CONNECT_PROMPT = 0x00000010;
  211.                 private const int CONNECT_UPDATE_PROFILE = 0x00000001;
  212.                 //IE4+
  213.                 private const int CONNECT_REDIRECT = 0x00000080;
  214.                 //NT5 only
  215.                 private const int CONNECT_COMMANDLINE = 0x00000800;
  216.                 private const int CONNECT_CMD_SAVECRED = 0x00001000;
  217.                
  218.                 #endregion
  219.  
  220.                 #region Propertys and options
  221.                 private bool lf_SaveCredentials = false;
  222.                 /// <summary>
  223.                 /// Option to save credentials are reconnection...
  224.                 /// </summary>
  225.                 public bool SaveCredentials{
  226.                         get{return(lf_SaveCredentials);}
  227.                         set{lf_SaveCredentials=value;}
  228.                 }
  229.                 private bool lf_Persistent = false;
  230.                 /// <summary>
  231.                 /// Option to reconnect drive after log off / reboot ...
  232.                 /// </summary>
  233.                 public bool Persistent{
  234.                         get{return(lf_Persistent);}
  235.                         set{lf_Persistent=value;}
  236.                 }
  237.                 private bool lf_Force = false;
  238.                 /// <summary>
  239.                 /// Option to force connection if drive is already mapped...
  240.                 /// or force disconnection if network path is not responding...
  241.                 /// </summary>
  242.                 public bool Force{
  243.                         get{return(lf_Force);}
  244.                         set{lf_Force=value;}
  245.                 }
  246.                 private bool ls_PromptForCredentials = false;
  247.                 /// <summary>
  248.                 /// Option to prompt for user credintals when mapping a drive
  249.                 /// </summary>
  250.                 public bool PromptForCredentials{
  251.                         get{return(ls_PromptForCredentials);}
  252.                         set{ls_PromptForCredentials=value;}
  253.                 }
  254.  
  255.                 private string ls_Drive = "s:";
  256.                 /// <summary>
  257.                 /// Drive to be used in mapping / unmapping...
  258.                 /// </summary>
  259.                 public string LocalDrive{
  260.                         get{return(ls_Drive);}
  261.                         set{
  262.                                 if(value.Length>=1) {
  263.                                         ls_Drive=value.Substring(0,1)+":";
  264.                                 }else{
  265.                                         ls_Drive="";
  266.                                 }
  267.                         }
  268.                 }
  269.                 private string ls_ShareName = "\\\\Computer\\C$";
  270.                 /// <summary>
  271.                 /// Share address to map drive to.
  272.                 /// </summary>
  273.                 public string ShareName{
  274.                         get{return(ls_ShareName);}
  275.                         set{ls_ShareName=value;}
  276.                 }
  277.                 #endregion
  278.  
  279.                 #region Function mapping
  280.                 /// <summary>
  281.                 /// Map network drive
  282.                 /// </summary>
  283.                 public void MapDrive(){zMapDrive(null, null);}
  284.                 /// <summary>
  285.                 /// Map network drive (using supplied Password)
  286.                 /// </summary>
  287.                 public void MapDrive(string Password){zMapDrive(null, Password);}
  288.                 /// <summary>
  289.                 /// Map network drive (using supplied Username and Password)
  290.                 /// </summary>
  291.                 public void MapDrive(string Username, string Password){zMapDrive(Username, Password);}
  292.                 /// <summary>
  293.                 /// Unmap network drive
  294.                 /// </summary>
  295.                 public void UnMapDrive(){zUnMapDrive(this.lf_Force);}
  296.                 /// <summary>
  297.                 /// Check / restore persistent network drive
  298.                 /// </summary>
  299.                 public void RestoreDrives(){zRestoreDrive();}
  300.                 /// <summary>
  301.                 /// Display windows dialog for mapping a network drive
  302.                 /// </summary>
  303.                 public void ShowConnectDialog(Form ParentForm){zDisplayDialog(ParentForm,1);}
  304.                 /// <summary>
  305.                 /// Display windows dialog for disconnecting a network drive
  306.                 /// </summary>
  307.                 public void ShowDisconnectDialog(Form ParentForm){zDisplayDialog(ParentForm,2);}
  308.  
  309.  
  310.                 #endregion
  311.  
  312.                 #region Core functions
  313.  
  314.                 // Map network drive
  315.                 private void zMapDrive(string psUsername, string psPassword){
  316.                         //create struct data
  317.                         structNetResource stNetRes = new structNetResource();                  
  318.                         stNetRes.iScope=2;
  319.                         stNetRes.iType=RESOURCETYPE_DISK;
  320.                         stNetRes.iDisplayType=3;
  321.                         stNetRes.iUsage=1;
  322.                         stNetRes.sRemoteName=ls_ShareName;
  323.                         stNetRes.sLocalName=ls_Drive;                  
  324.                         //prepare params
  325.                         int iFlags=0;
  326.                         if(lf_SaveCredentials){iFlags+=CONNECT_CMD_SAVECRED;}
  327.                         if(lf_Persistent){iFlags+=CONNECT_UPDATE_PROFILE;}
  328.                         if(ls_PromptForCredentials){iFlags+=CONNECT_INTERACTIVE+CONNECT_PROMPT;}
  329.                         if(psUsername==""){psUsername=null;}
  330.                         if(psPassword==""){psPassword=null;}
  331.                         //if force, unmap ready for new connection
  332.                         if(lf_Force){try{zUnMapDrive(true);}catch{}}
  333.                         //call and return
  334.                         int i = WNetAddConnection2A(ref stNetRes, psPassword, psUsername, iFlags);
  335.             // SECTION UNSTABLE if letter in use fails!
  336.             //if (i > 0) { throw new System.ComponentModel.Win32Exception(i); }                                        
  337.                 }
  338.  
  339.  
  340.                 // Unmap network drive 
  341.                 private void zUnMapDrive(bool pfForce){
  342.                         //call unmap and return
  343.                         int iFlags=0;
  344.                         if(lf_Persistent){iFlags+=CONNECT_UPDATE_PROFILE;}
  345.                         int i = WNetCancelConnection2A(ls_Drive, iFlags, Convert.ToInt32(pfForce));
  346.                         if(i!=0) i=WNetCancelConnection2A(ls_ShareName, iFlags, Convert.ToInt32(pfForce));  //disconnect if localname was null
  347.                         if(i>0){throw new System.ComponentModel.Win32Exception(i);}
  348.                 }
  349.                
  350.  
  351.                 // Check / Restore a network drive
  352.                 private void zRestoreDrive()
  353.                 {
  354.                         //call restore and return
  355.                         int i = WNetRestoreConnectionW(0, null);
  356.                         if(i>0){throw new System.ComponentModel.Win32Exception(i);}
  357.                 }
  358.  
  359.                 // Display windows dialog
  360.                 private void zDisplayDialog(Form poParentForm, int piDialog)
  361.                 {
  362.                         int i = -1;
  363.                         int iHandle = 0;
  364.                         //get parent handle
  365.                         if(poParentForm!=null)
  366.                         {
  367.                                 iHandle = poParentForm.Handle.ToInt32();
  368.                         }
  369.                         //show dialog
  370.                         if(piDialog==1)
  371.                         {
  372.                                 i = WNetConnectionDialog(iHandle, RESOURCETYPE_DISK);
  373.                         }else if(piDialog==2)
  374.                         {
  375.                                 i = WNetDisconnectDialog(iHandle, RESOURCETYPE_DISK);
  376.                         }
  377.                         if(i>0){throw new System.ComponentModel.Win32Exception(i);}
  378.                         //set focus on parent form
  379.                         poParentForm.BringToFront();
  380.                 }
  381.  
  382.  
  383.                 #endregion
  384.  
  385.         }
  386.  
  387. }