1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
public static DataTable InquireShareFile() { try { ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from win32_share"); DataTable ShareFile = new DataTable(); ShareFile.Columns.Add("name"); ShareFile.Columns.Add("path"); ShareFile.Columns.Add("permissions"); ShareFile.Columns.Add("type"); foreach (ManagementObject share in searcher.Get()) { try { string name = share["Name"].ToString(); string path = share["Path"].ToString(); string type = share["Type"].ToString(); if (type == "0") { type = "磁盘驱动器"; } else if (type == "1") { type = "打印队列"; } else if (type == "2") { type = "设备"; } else if (type == "3") { type = "IPC"; } else if (type == "2147483648") { type = "磁盘驱动器管理"; } else if (type == "2147483649") { type = "打印队列管理"; } else if (type == "2147483650") { type = "设备管理"; } else if (type == "2147483651") { type = "IPC 管理员"; } string Permissions = ""; string cmd = string.Format("net share {0}", name); string strOutput = ImplementationCMD(cmd); if (strOutput.IndexOf("FULL") > -1) { Permissions = "完全控制"; } else if (strOutput.IndexOf("READ") > -1) { Permissions = "只读"; } else if (strOutput.IndexOf("CHANGE") > -1) { Permissions = "读取/写入"; } DataRow dr = ShareFile.NewRow(); dr["name"] = name; dr["path"] = path; dr["permissions"] = Permissions; dr["type"] = type; ShareFile.Rows.Add(dr); } catch (Exception ex) { TXTHelper.Logs(ex.ToString()); return null; } } return ShareFile; } catch (Exception ex) { TXTHelper.Logs(ex.ToString()); return null; } }
public static bool AddShareFolder(string FolderPath, string ShareName, string Permissions) { try { string cmd = string.Format(@"net share {0}={1} /grant:{2},{3}", ShareName, FolderPath, System.Environment.UserName, Permissions); string strOutput = ImplementationCMD(cmd); return strOutput.IndexOf("共享成功") > -1 ? true : false; } catch (Exception ex) { TXTHelper.Logs(ex.ToString()); return false; } }
public static bool AddShareFolder(string FolderPath, string ShareName, SharingPermissions.PermissionsType PermissionsType) { try { string cmd = string.Format(@"net share {0}={1} /grant:{2},{3}", ShareName, FolderPath, System.Environment.UserName, PermissionsType); string strOutput = ImplementationCMD(cmd); return strOutput.IndexOf("共享成功") > -1 ? true : false; } catch (Exception ex) { TXTHelper.Logs(ex.ToString()); return false; } }
public static bool DeleteShareFolder(string FolderPath) { try { string cmd = string.Format(@"net share {0} /delete /y", FolderPath); string strOutput = ImplementationCMD(cmd); return strOutput.IndexOf("已经删除") > -1 ? true : false; } catch (Exception ex) { TXTHelper.Logs(ex.ToString()); return false; } }
private static string ImplementationCMD(string cmd) { try { System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; p.Start(); p.StandardInput.WriteLine(cmd); p.StandardInput.AutoFlush = true; p.StandardInput.WriteLine("exit"); string strOutput = p.StandardOutput.ReadToEnd(); p.WaitForExit(); p.Close(); return strOutput; } catch (Exception ex) { TXTHelper.Logs(ex.ToString()); return null; } }
|