private void btnSave_Click(object sender, EventArgs e) { string directory = @"\\kdthk-dm1\project\KDTHK-DM\" + AdUtil.getAccount("kmhk.local"); List <string> queryList = new List <string>(); foreach (DataGridViewRow row in dgvCopySetup.Rows) { string fileName = row.Cells[1].Value.ToString(); string keyword = row.Cells[2].Value.ToString(); string favSelection = row.Cells[3].Value.ToString(); string filePath = row.Cells[4].Value.ToString(); string folder = row.Cells[6].Value.ToString(); string shared = row.Cells[7].Value.ToString(); string extension = Path.GetExtension(filePath); string favorite = favSelection == "---" ? "False" : "True"; //if (!Directory.Exists(directory + folder)) // Directory.CreateDirectory(directory + folder); string destination = Path.Combine(directory, fileName + extension); File.Copy(filePath, destination, true); FileInfo info = new FileInfo(destination); FileSecurity fs = info.GetAccessControl(); AuthorizationRuleCollection rules = fs.GetAccessRules(true, true, typeof(NTAccount)); string lastmodified = info.LastWriteTime.ToString("yyyy/MM/dd HH:mm:ss"); string now = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"); fs.SetAccessRuleProtection(true, false); fs.AddAccessRule(new FileSystemAccessRule(@"kmhk\itadmin", FileSystemRights.FullControl, AccessControlType.Allow)); fs.AddAccessRule(new FileSystemAccessRule(AdUtil.GetUserIdByUsername(GlobalService.User, "kmhk.local"), FileSystemRights.FullControl, AccessControlType.Allow)); string storedDest = destination; if (storedDest.Contains("'")) { storedDest = storedDest.Replace("'", "''"); } if (fileName.Contains("'")) { fileName = fileName.Replace("'", "''"); } if (keyword.Contains("'")) { keyword = keyword.Replace("'", "''"); } if (shared != "-") { List <string> fileSharedList = shared.Split(';').ToList(); List <string> hklist = new List <string>(); List <string> cnlist = new List <string>(); List <string> vnlist = new List <string>(); List <string> jplist = new List <string>(); foreach (string item in fileSharedList) { if (UserUtil.IsCnMember(item.Trim())) { cnlist.Add(item.Trim()); } else if (UserUtil.IsVnMember(item.Trim())) { vnlist.Add(item.Trim()); } else if (UserUtil.IsJpMember(item.Trim())) { jplist.Add(item.Trim()); } else { hklist.Add(item.Trim()); } } foreach (string fileShared in hklist) { string sharedId = AdUtil.GetUserIdByUsername(fileShared.Trim(), "kmhk.local"); string tableName = "TB_" + sharedId; fs.AddAccessRule(new FileSystemAccessRule(sharedId, FileSystemRights.Modify, AccessControlType.Allow)); if (UserUtil.IsSpecialUser(fileShared)) //if (fileShared == "Chow Chi To(周志滔,Sammy)" || fileShared == "Ling Wai Man(凌慧敏,Velma)" || fileShared == "Chan Fai Lung(陳輝龍,Onyx)" || fileShared == "Ng Lau Yu, Lilith (吳柳如)" || // fileShared == "Lee Miu Wah(李苗華)" || fileShared == "Lee Ming Fung(李銘峯)" || fileShared == "Ho Kin Hang(何健恒,Ken)" || fileShared == "Yeung Wai, Gabriel (楊偉)") { string asText = string.Format("select as_userid from TB_USER_AS where as_user = N'{0}'", fileShared.Trim()); string asId = DataService.GetInstance().ExecuteScalar(asText).ToString().Trim(); fs.AddAccessRule(new FileSystemAccessRule(asId, FileSystemRights.Modify, AccessControlType.Allow)); } string sharedDivision = SystemUtil.GetDivision(fileShared.Trim()); string sharedDepartment = SystemUtil.GetDepartment(fileShared.Trim()); string sharedVpath = sharedDivision != GlobalService.Division && folder.StartsWith(@"\" + GlobalService.Division) ? @"\Documents" + folder : sharedDepartment != GlobalService.Department && folder.StartsWith(@"\Common") ? @"\Documents" + folder : folder; if (sharedVpath.Contains("'")) { sharedVpath = sharedVpath.Replace("'", "''"); } string sharedText = string.Format("insert into " + tableName + " (r_filename, r_extension, r_keyword, r_lastaccess, r_lastmodified, r_owner, r_shared, r_path, r_vpath, r_deletedate)" + " values (N'{0}', '{1}', N'{2}', '{3}', '{4}', N'{5}', N'{6}', N'{7}', N'{8}', '{9}')", fileName, extension, keyword, now, lastmodified, GlobalService.User, fileShared.Trim(), storedDest, sharedVpath, "2099/12/31"); queryList.Add(sharedText); } try { File.SetAccessControl(destination, fs); } catch (Exception ex) { Debug.WriteLine(ex.Message + ex.StackTrace); continue; } if (cnlist.Count > 0) { PermissionUtil.SetGlobalPermission(cnlist, destination, "kmcn.local"); SharedUtil.SharedCN(cnlist, storedDest, fileName, keyword); } if (vnlist.Count > 0) { PermissionUtil.SetGlobalPermission(vnlist, destination, "kdtvn.local"); SharedUtil.SharedVN(vnlist, storedDest, fileName, keyword); } if (jplist.Count > 0) { PermissionUtil.SetGlobalPermission(jplist, destination, "km.local"); SharedUtil.SharedJp(jplist, storedDest, fileName, keyword); } try { List <string> receiverlist = cnlist.Concat(vnlist).Concat(jplist).ToList(); if (receiverlist.Count > 0) { EmailUtil.SendNotificationEmail(receiverlist); } } catch (Exception ex) { Debug.WriteLine(ex.Message + ex.StackTrace); } } GlobalService.RootTable.Rows.Add(fileName, keyword, lastmodified, now, GlobalService.User, shared, destination, folder, 0, favorite, "True", "False"); if (folder.Contains("'")) { folder = folder.Replace("'", "''"); } if (shared == "") { shared = "-"; } string ownerText = string.Format("insert into " + GlobalService.DbTable + " (r_filename, r_extension, r_keyword, r_lastaccess, r_lastmodified, r_owner, r_shared, r_path, r_vpath, r_deletedate)" + " values (N'{0}', '{1}', N'{2}', '{3}', '{4}', N'{5}', N'{6}', N'{7}', N'{8}', '{9}')", fileName, extension, keyword, now, lastmodified, GlobalService.User, shared, storedDest, folder, "2099/12/31"); queryList.Add(ownerText); } foreach (string text in queryList) { DataService.GetInstance().ExecuteNonQuery(text); } //DataUtil.SyncDataToServer(); GlobalService.RootTable = RootUtil.RootDataTable(); this.DialogResult = DialogResult.OK; }
private void AddData(List <UploadFileList> uploadList, string defaultPath) { foreach (UploadFileList item in uploadList) { string fileName = item.FileName; string filePath = item.FilePath; string mode = item.Mode; string dragMode = item.DragMode; string favoriteMode = item.Favorite.Trim() == "True" ? "Yes" : "---"; string folderName = item.FolderName; Debug.WriteLine(fileName); if (fileName.Contains("thumbs.db") || fileName.Contains("Thumbs.db")) { continue; } string MyDocumentPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); string MyPicturePath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures); string DesktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); string DownloadPath = ""; SHGetKnownFolderPath(Downloads, 0, IntPtr.Zero, out DownloadPath); string departmentFolder = GlobalService.DepartmentFolder; string directory = filePath.StartsWith(@"M:\") ? filePath.Replace(@"M:\", @"\\kdthk-dm1\project\") : filePath.StartsWith(@"L:\") ? filePath.Replace(@"L:\", departmentFolder + @"\") : filePath.StartsWith(@"\\kdthk-dm1\Project\") ? filePath.Replace("Project", "project") : filePath.StartsWith(@"\\kdthk-dm1\PROJECT\") ? filePath.Replace("PROJECT", "project") : filePath.StartsWith(@"\\kdthk-dm1") ? filePath : @"\\kdthk-dm1\project\KDTHK-DM\" + AdUtil.getAccount() + @"\" + fileName; string tidyFilePath = filePath.StartsWith(@"\\kdthk-dm1\department\mcc\common") ? filePath.Replace(@"\\kdthk-dm1\department\mcc\common", @"\\kdthk-dm1\mcc$\Common") : filePath.StartsWith(@"\\kdthk-dm1\Department\MCC\common") ? filePath.Replace(@"\\kdthk-dm1\Department\MCC\common", @"\\kdthk-dm1\mcc$\Common") : filePath.StartsWith(@"\\kdthk-dm1\department\rps") ? filePath.Replace(@"\\kdthk-dm1\department\rps", @"\\kdthk-dm1\rps$") : filePath.StartsWith(@"\\kdthk-dm1\department\ga") ? filePath.Replace(@"\\kdthk-dm1\department\ga", @"\\kdthk-dm1\ga$") : filePath.StartsWith(@"\\kdthk-dm1\department\cm") ? filePath.Replace(@"\\kdthk-dm1\department\cm", @"\\kdthk-dm1\cm$") : filePath.StartsWith(@"\\kdthk-dm1\department\ipo purchasing") ? filePath.Replace(@"\\kdthk-dm1\department\ipo purchasing", @"\\kdthk-dm1\ipo$") : filePath.StartsWith(@"\\kdthk-dm1\department\logistics") ? filePath.Replace(@"\\kdthk-dm1\department\logistics", @"\\kdthk-dm1\logistics$") : filePath; if (defaultPath.StartsWith(@"\Favorite")) { defaultPath = defaultPath.Replace(@"\Favorite", ""); } string structure = @"\Documents"; if (tidyFilePath.StartsWith(MyDocumentPath)) { structure = tidyFilePath.Replace(MyDocumentPath, ""); if (dragMode == "folder") { structure = structure.Substring(0, structure.LastIndexOf("\\")); //if (structure.Contains("\\")) // structure = structure.Substring(structure.LastIndexOf("\\")); } else { structure = ""; } structure = defaultPath + structure; } else if (tidyFilePath.StartsWith(MyPicturePath)) { structure = tidyFilePath.Replace(MyPicturePath, ""); if (dragMode == "folder") { structure = structure.Substring(0, structure.LastIndexOf("\\")); //if (structure.Contains("\\")) // structure = structure.Substring(structure.LastIndexOf("\\")); } else { structure = ""; } structure = defaultPath + structure; } else if (tidyFilePath.StartsWith(DownloadPath)) { structure = tidyFilePath.Replace(DownloadPath, ""); if (dragMode == "folder") { structure = structure.Substring(0, structure.LastIndexOf("\\")); //if (structure.Contains("\\")) // structure = structure.Substring(structure.LastIndexOf("\\")); } else { structure = ""; } structure = defaultPath + structure; } else if (tidyFilePath.StartsWith(DesktopPath)) { if (tidyFilePath.Contains("MyCloud Sync")) { structure = tidyFilePath.Replace(DesktopPath + @"\MyCloud Sync", ""); } else { structure = tidyFilePath.Replace(DesktopPath, ""); } if (dragMode == "folder") { structure = structure.Substring(0, structure.LastIndexOf("\\")); //structure = structure.Substring(structure.IndexOf(folderName)); } else { structure = ""; } structure = defaultPath + structure; } else if (tidyFilePath.StartsWith(@"M:\")) { structure = tidyFilePath.Replace(@"M:\", ""); if (dragMode == "folder") { structure = structure.Substring(0, structure.LastIndexOf("\\")); if (!structure.StartsWith("\\")) { structure = "\\" + structure; } } else { structure = ""; } structure = defaultPath + structure; } else if (tidyFilePath.StartsWith(@"\\kdthk-dm1\project\") || tidyFilePath.StartsWith(@"\\kdthk-dm1\Project\") || tidyFilePath.StartsWith(@"\\kdthk-dm1\PROJECT\")) { tidyFilePath = tidyFilePath.StartsWith(@"\\kdthk-dm1\Project\") ? tidyFilePath.Replace("Project", "project") : tidyFilePath.StartsWith(@"\\kdthk-dm1\PROJECT\") ? tidyFilePath.Replace("PROJECT", "project") : tidyFilePath; structure = tidyFilePath.Replace(@"\\kdthk-dm1\project\", ""); if (dragMode == "folder") { structure = structure.Substring(0, structure.LastIndexOf("\\")); if (!structure.StartsWith("\\")) { structure = "\\" + structure; } } else { structure = ""; } structure = defaultPath + structure; } else if (tidyFilePath.StartsWith(@"L:\")) { structure = tidyFilePath.Replace(@"L:", ""); if (dragMode == "folder") { structure = structure.Substring(0, structure.LastIndexOf("\\")); if (!structure.StartsWith("\\")) { structure = "\\" + structure; } } else { structure = ""; } structure = defaultPath + structure; } else if (tidyFilePath.StartsWith(@"C:\")) { structure = tidyFilePath.Replace(@"C:", ""); if (dragMode == "folder") { structure = structure.Substring(0, structure.LastIndexOf("\\")); //if (structure.Contains("\\")) // structure = structure.Substring(structure.LastIndexOf("\\")); if (!structure.StartsWith("\\")) { structure = "\\" + structure; } } else { structure = ""; } structure = defaultPath + structure; } else if (tidyFilePath.StartsWith(@"E:\")) { structure = tidyFilePath.Replace(@"E:", ""); if (dragMode == "folder") { structure = structure.Substring(0, structure.LastIndexOf("\\")); //if (structure.Contains("\\")) // structure = structure.Substring(structure.LastIndexOf("\\")); if (!structure.StartsWith("\\")) { structure = "\\" + structure; } } else { structure = ""; } structure = defaultPath + structure; } else if (tidyFilePath.StartsWith(departmentFolder)) { structure = tidyFilePath.Replace(departmentFolder, ""); structure = structure.Substring(1); if (dragMode == "folder") { structure = structure.Substring(0, structure.LastIndexOf("\\")); //if (structure.Contains("\\")) // structure = structure.Substring(structure.LastIndexOf("\\")); if (!structure.StartsWith("\\")) { structure = "\\" + structure; } } else { structure = ""; } structure = defaultPath + structure; } else { structure = tidyFilePath.Replace(@"D:\", ""); if (dragMode == "folder") { if (structure.Contains(@"\")) { structure = structure.Substring(0, structure.LastIndexOf("\\")); //if (structure.Contains("\\")) // structure = structure.Substring(structure.LastIndexOf("\\")); structure = defaultPath + structure; } else { structure = defaultPath; } } else { structure = defaultPath; } } if (structure.EndsWith(@"\")) { structure = structure.Substring(0, structure.Length - 1); } if (structure.Contains(@"\\")) { structure.Replace(@"\\", @"\"); } if (fileName.Contains('.')) { fileName = fileName.Substring(0, fileName.LastIndexOf('.')); } if (mode == "rename") { int seq = 1; string fileNameOnly = Path.GetFileNameWithoutExtension(directory); string extensionOnly = Path.GetExtension(directory); string pathOnly = Path.GetDirectoryName(directory); string newPath = directory; string tmpFile1 = ""; string tmpFile2 = ""; while (File.Exists(newPath)) { int tmpCount = seq; if (tmpFile1.Contains("(" + tmpCount + ")")) { tmpFile1 = tmpFile1.Substring(0, tmpFile1.LastIndexOf("(" + tmpCount + ")")); } tmpFile2 = string.Format("{0}({1})", tmpFile1, seq++); newPath = Path.Combine(pathOnly, tmpFile2 + extensionOnly); } dgvDocSetup.Rows.Add(tmpFile2, "", favoriteMode, "---", filePath, newPath, structure, "Rename", "-"); } else if (mode == "overwrite") { dgvDocSetup.Rows.Add(fileName, "", favoriteMode, "---", filePath, directory, structure, "Overwrite", "-"); } else { dgvDocSetup.Rows.Add(fileName, "", favoriteMode, "---", filePath, directory, structure, "Normal", "-"); } } List <int> indexList = new List <int>(); foreach (DataGridViewRow row in dgvDocSetup.Rows) { string itemMode = row.Cells[6].Value.ToString(); if (itemMode != "Rename") { indexList.Add(row.Index); } } foreach (int idx in indexList) { dgvDocSetup.Rows[idx].Cells[0].ReadOnly = true; } }