Advertisement
Guest User

Untitled

a guest
Aug 31st, 2017
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.15 KB | None | 0 0
  1. HKEY_LOCAL_MACHINESystemCurrentControlSetControlSession Manager
  2.  
  3. ??C:Long Directory NameLong File Name.exe
  4.  
  5. mv %WINDIR%System32mfc42.dll %WINDIRSystem32mfc42.dll.old
  6. Install new mfc42.dll
  7. Tell user to save work and restart applications
  8.  
  9. using System.Runtime.InteropServices;
  10. using System.Diagnostics;
  11.  
  12. static public class FileUtil
  13. {
  14. [StructLayout(LayoutKind.Sequential)]
  15. struct RM_UNIQUE_PROCESS
  16. {
  17. public int dwProcessId;
  18. public System.Runtime.InteropServices.ComTypes.FILETIME ProcessStartTime;
  19. }
  20.  
  21. const int RmRebootReasonNone = 0;
  22. const int CCH_RM_MAX_APP_NAME = 255;
  23. const int CCH_RM_MAX_SVC_NAME = 63;
  24.  
  25. enum RM_APP_TYPE
  26. {
  27. RmUnknownApp = 0,
  28. RmMainWindow = 1,
  29. RmOtherWindow = 2,
  30. RmService = 3,
  31. RmExplorer = 4,
  32. RmConsole = 5,
  33. RmCritical = 1000
  34. }
  35.  
  36. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  37. struct RM_PROCESS_INFO
  38. {
  39. public RM_UNIQUE_PROCESS Process;
  40.  
  41. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCH_RM_MAX_APP_NAME + 1)]
  42. public string strAppName;
  43.  
  44. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCH_RM_MAX_SVC_NAME + 1)]
  45. public string strServiceShortName;
  46.  
  47. public RM_APP_TYPE ApplicationType;
  48. public uint AppStatus;
  49. public uint TSSessionId;
  50. [MarshalAs(UnmanagedType.Bool)]
  51. public bool bRestartable;
  52. }
  53.  
  54. [DllImport("rstrtmgr.dll", CharSet = CharSet.Unicode)]
  55. static extern int RmRegisterResources(uint pSessionHandle,
  56. UInt32 nFiles,
  57. string[] rgsFilenames,
  58. UInt32 nApplications,
  59. [In] RM_UNIQUE_PROCESS[] rgApplications,
  60. UInt32 nServices,
  61. string[] rgsServiceNames);
  62.  
  63. [DllImport("rstrtmgr.dll", CharSet = CharSet.Auto)]
  64. static extern int RmStartSession(out uint pSessionHandle, int dwSessionFlags, string strSessionKey);
  65.  
  66. [DllImport("rstrtmgr.dll")]
  67. static extern int RmEndSession(uint pSessionHandle);
  68.  
  69. [DllImport("rstrtmgr.dll")]
  70. static extern int RmGetList(uint dwSessionHandle,
  71. out uint pnProcInfoNeeded,
  72. ref uint pnProcInfo,
  73. [In, Out] RM_PROCESS_INFO[] rgAffectedApps,
  74. ref uint lpdwRebootReasons);
  75.  
  76. /// <summary>
  77. /// Find out what process(es) have a lock on the specified file.
  78. /// </summary>
  79. /// <param name="path">Path of the file.</param>
  80. /// <returns>Processes locking the file</returns>
  81. /// <remarks>See also:
  82. /// http://msdn.microsoft.com/en-us/library/windows/desktop/aa373661(v=vs.85).aspx
  83. /// http://wyupdate.googlecode.com/svn-history/r401/trunk/frmFilesInUse.cs (no copyright in code at time of viewing)
  84. ///
  85. /// </remarks>
  86. static public List<Process> WhoIsLocking(string path)
  87. {
  88. uint handle;
  89. string key = Guid.NewGuid().ToString();
  90. List<Process> processes = new List<Process>();
  91.  
  92. int res = RmStartSession(out handle, 0, key);
  93. if (res != 0) throw new Exception("Could not begin restart session. Unable to determine file locker.");
  94.  
  95. try
  96. {
  97. const int ERROR_MORE_DATA = 234;
  98. uint pnProcInfoNeeded = 0,
  99. pnProcInfo = 0,
  100. lpdwRebootReasons = RmRebootReasonNone;
  101.  
  102. string[] resources = new string[] { path }; // Just checking on one resource.
  103.  
  104. res = RmRegisterResources(handle, (uint)resources.Length, resources, 0, null, 0, null);
  105.  
  106. if (res != 0) throw new Exception("Could not register resource.");
  107.  
  108. //Note: there's a race condition here -- the first call to RmGetList() returns
  109. // the total number of process. However, when we call RmGetList() again to get
  110. // the actual processes this number may have increased.
  111. res = RmGetList(handle, out pnProcInfoNeeded, ref pnProcInfo, null, ref lpdwRebootReasons);
  112.  
  113. if (res == ERROR_MORE_DATA)
  114. {
  115. // Create an array to store the process results
  116. RM_PROCESS_INFO[] processInfo = new RM_PROCESS_INFO[pnProcInfoNeeded];
  117. pnProcInfo = pnProcInfoNeeded;
  118.  
  119. // Get the list
  120. res = RmGetList(handle, out pnProcInfoNeeded, ref pnProcInfo, processInfo, ref lpdwRebootReasons);
  121. if (res == 0)
  122. {
  123. processes = new List<Process>((int)pnProcInfo);
  124.  
  125. // Enumerate all of the results and add them to the
  126. // list to be returned
  127. for (int i = 0; i < pnProcInfo; i++)
  128. {
  129. try
  130. {
  131. processes.Add(Process.GetProcessById(processInfo[i].Process.dwProcessId));
  132. }
  133. // catch the error -- in case the process is no longer running
  134. catch (ArgumentException) { }
  135. }
  136. }
  137. else throw new Exception("Could not list processes locking resource.");
  138. }
  139. else if (res != 0) throw new Exception("Could not list processes locking resource. Failed to get size of result.");
  140. }
  141. finally
  142. {
  143. RmEndSession(handle);
  144. }
  145.  
  146. return processes;
  147. }
  148. }
  149.  
  150. string[] files = Directory.GetFiles(target_dir);
  151. List<Process> lstProcs = new List<Process>();
  152.  
  153. foreach (string file in files)
  154. {
  155. lstProcs = ProcessHandler.WhoIsLocking(file);
  156. if (lstProcs.Count > 0) // deal with the file lock
  157. {
  158. foreach (Process p in lstProcs)
  159. {
  160. if (p.MachineName == ".")
  161. ProcessHandler.localProcessKill(p.ProcessName);
  162. else
  163. ProcessHandler.remoteProcessKill(p.MachineName, txtUserName.Text, txtPassword.Password, p.ProcessName);
  164. }
  165. File.Delete(file);
  166. }
  167. else
  168. File.Delete(file);
  169. }
  170.  
  171. public static void localProcessKill(string processName)
  172. {
  173. foreach (Process p in Process.GetProcessesByName(processName))
  174. {
  175. p.Kill();
  176. }
  177. }
  178.  
  179. public static void remoteProcessKill(string computerName, string fullUserName, string pword, string processName)
  180. {
  181. var connectoptions = new ConnectionOptions();
  182. connectoptions.Username = fullUserName; // @"YourDomainNameUserName";
  183. connectoptions.Password = pword;
  184.  
  185. ManagementScope scope = new ManagementScope(@"\" + computerName + @"rootcimv2", connectoptions);
  186.  
  187. // WMI query
  188. var query = new SelectQuery("select * from Win32_process where name = '" + processName + "'");
  189.  
  190. using (var searcher = new ManagementObjectSearcher(scope, query))
  191. {
  192. foreach (ManagementObject process in searcher.Get())
  193. {
  194. process.InvokeMethod("Terminate", null);
  195. process.Dispose();
  196. }
  197. }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement