//Adds a directory (through add) public bool AddDirectory(string sDisplayName, string sPath, string sUNCUsername, string sUNCPassword) { //Cannot add a zero length if (sDisplayName.Length == 0 || sPath.Length == 0) { return(false); } //Ensure a backslash at the end if (sPath.Substring(sPath.Length - 1) != "\\") { sPath += '\\'; } BasePathsDb aDir = new BasePathsDb { BasePath = sPath, DisplayName = sDisplayName, Username = sUNCUsername, Password = sUNCPassword }; LiteCollection <BasePathsDb> aDBValues = m_db.GetCollection <BasePathsDb>("dirs"); // Use Linq to query documents var results = aDBValues.FindOne(x => x.BasePath == sPath); if (results != null) //If path exists, return { return(false); //Already exist } else //Add new dir { //Now add to DB aDBValues.EnsureIndex(x => x.DisplayName); aDBValues.Insert(aDir); //Connect & track the UNC for this path if (aDir.BasePath.IndexOf(@"\\") != -1) { ConnectToUNC(aDir); } } return(true); }
private void ConnectToUNC(BasePathsDb baseDb) { //Path is \\<server>@<username>:<password>\rest\of\the\path // Regex x = new Regex(@"\\\\(?<server>.*?)@(?<user>.*?):(?<pass>.*?)\\(?<path>.*?)$", RegexOptions.IgnoreCase); // MatchCollection mc = x.Matches(aBasePath.sBasePath); // // if (mc.Count != 0) // { // string sServer = mc[0].Groups["server"].Value; // string sUser = mc[0].Groups["user"].Value; // string sPassword = mc[0].Groups["pass"].Value; // string sPath = mc[0].Groups["path"].Value; // aBasePath.sBasePath = @"\\" + sServer + @"\" + sPath; //Create a UNC connector UNCAccess pUNC = new UNCAccess(); //to be added in array based on ID //Fire up a task to connect var bgw = new BackgroundWorker(); bgw.DoWork += (_, __) => { if (!pUNC.login(baseDb.BasePath, baseDb.Username, "", baseDb.Password)) { Console.WriteLine("Could not connect to: " + baseDb.BasePath); } }; bgw.RunWorkerCompleted += (_, __) => { Console.WriteLine("Finished connecting to UNC: " + baseDb.BasePath); }; bgw.RunWorkerAsync(); //Add the entry to the list of UNCs BasePathsUNC sUNC = new BasePathsUNC { pUNC = pUNC, Id = baseDb.Id }; aUNCConnections.Add(sUNC); }
//Removes a directory based on the id in list public bool RemoveDirectory(int nID) { LiteCollection <BasePathsDb> aDBValues = m_db.GetCollection <BasePathsDb>("dirs"); BasePathsDb results = aDBValues.FindOne(x => x.Id == nID); if (results == null) { return(false); } //Check if id has a UNC for (int i = 0; i < aUNCConnections.Count; i++) { if (((BasePathsUNC)aUNCConnections[i]).Id == results.Id) { ((BasePathsUNC)aUNCConnections[i]).pUNC.NetUseDelete(); aUNCConnections.RemoveAt(i); break; } } //Remove the data at given ID aDBValues.Delete(results.Id); return(true); }