/// <summary> /// Copies one directory, all files, and all sub directories to another directory. /// </summary> /// <param name="sourceDirectory">Directory to copy files and sub-directories from. </param> /// <param name="destinationDirectory">Directory to copy files and sub-directories to. </param> /// <param name="overwriteFlag">Determines whether or not to overwrite a file if it already exists at the given location</param> internal static void CopyDirectories(string sourceDirectory, string destinationDirectory, bool overwriteFlag, bool useImpersonation) { SecurityACL dacl = null; try { sourceDirectory = SharedSupport.AddBackSlashToDirectory(sourceDirectory); destinationDirectory = SharedSupport.AddBackSlashToDirectory(destinationDirectory); if (!Directory.Exists(sourceDirectory)) { //If the source directory does not exist throw a new error. throw new DirectoryNotFoundException(SharedSupport.GetLocalizedString("StudentAssignment_DirectoryNotFound") + sourceDirectory); } if (!Directory.Exists(destinationDirectory)) { if (useImpersonation) { // Impersonate the AM User ImpersonateUser User = null; try { User = new ImpersonateUser(); // Login User.Logon(); // start impersonating User.Start(); //if the destination does not exist, create it. Directory.CreateDirectory(destinationDirectory); } finally { if (User != null) { // stop impersonating User.Stop(); User.Dispose(); } } } else { Directory.CreateDirectory(destinationDirectory); } } //copy each file in the current directory string[] f = Directory.GetFiles(sourceDirectory); dacl = new SecurityACL(Constants.AMUserName); for (int i = 0; i < f.Length; i++) { if (!File.Exists(f[i].ToString())) { throw new FileNotFoundException(sourceDirectory + f[i].ToString()); } else { string sourceFile = sourceDirectory + Path.GetFileName(f[i].ToString()); string destinationFile = destinationDirectory + Path.GetFileName(f[i].ToString()); File.Copy(sourceFile, destinationFile, overwriteFlag); dacl.ApplyACLToFile(destinationFile); } } // recursively copy each subdirectory in the current directory string[] d = Directory.GetDirectories(sourceDirectory); if (d.Length > 0) { for (int i = 0; i < d.Length; i++) { string sourceDir = d[i].ToString(); string destDir = destinationDirectory + d[i].ToString().Replace(sourceDirectory, String.Empty); if (!Directory.Exists(destDir)) { if (useImpersonation) { // Impersonate the AM User ImpersonateUser User = null; try { User = new ImpersonateUser(); // Login User.Logon(); // start impersonating User.Start(); //if the destination does not exist, create it. Directory.CreateDirectory(destDir); } finally { if (User != null) { // stop impersonating User.Stop(); User.Dispose(); } } } else { Directory.CreateDirectory(destDir); } } ServerAction.CopyDirectories(sourceDir, destDir, overwriteFlag, useImpersonation); } } } catch (Exception ex) { SharedSupport.HandleError(ex); } finally { if (dacl != null) { dacl.Dispose(); } } }
public void RunService() { //Spawn new process - invoking the compiledFile UserProcess oProcess = new UserProcess(); try { // set actual output file location actualOutputFileLocation = workingDirectory + actualOutputFile; //Set process start parameters ProcessStartInfo psi = new ProcessStartInfo(); psi.Arguments = commandLineParams; psi.FileName = compiledFileName; psi.WorkingDirectory = workingDirectory; psi.RedirectStandardOutput = true; psi.RedirectStandardError = false; psi.UseShellExecute = false; if (File.Exists(inputFileLocation)) { oProcess.InputFile = workingDirectory + Guid.NewGuid() + ".txt"; File.Copy(inputFileLocation, oProcess.InputFile, true); psi.RedirectStandardInput = true; try { SecurityACL dacl = new SecurityACL(Constants.AMUserName); dacl.ApplyACLToFile(oProcess.InputFile); } catch (Exception) { // Continue on. If we fail to apply the ACL, we may still be able // to autocheck (i.e. if user has set custom permissions. } } oProcess.StartInfo = psi; oProcess.OutputFile = actualOutputFileLocation; //Log start SharedSupport.LogMessage(SharedSupport.GetLocalizedString("StudentAssignment_CheckStart")); if (!oProcess.Run(processTime)) { throw new System.Exception(SharedSupport.GetLocalizedString("ServerAction_FailedCreateTestProcess")); } //wait to see if process comes back in time. Otherwise if it runs too long - kill it if (!oProcess.HasExited) { SharedSupport.LogMessage(SharedSupport.GetLocalizedString("StudentAssignment_CheckKilled")); throw new System.Exception(SharedSupport.GetLocalizedString("StudentAssignemtn_CheckExceededProcessTime")); } else { SharedSupport.LogMessage(SharedSupport.GetLocalizedString("StudentAssignment_CheckEnd")); } } catch (Exception ex) { checkSuccessful = false; errorString = ex.Message; SharedSupport.HandleError(ex); } }