/// <summary> /// Determines whether file exists /// </summary> /// <param name="filePath">The file path.</param> /// <returns></returns> public static bool FileExists(string filePath) { bool result = false; string shareLocation = Path.GetPathRoot(filePath); // The file exists on a server. Connect to that server first. NetworkConnection.AddConnection(shareLocation, GlobalSettings.Items.DomainAdminCredential); try { result = File.Exists(filePath); } finally { NetworkConnection.RemoveConnection(shareLocation, forceRemoval: true); } return(result); }
/// <summary> /// Copies the file. /// </summary> /// <param name="sourcePath">The source path.</param> /// <param name="destinationPath">The destination path.</param> public static void CopyFile(string sourcePath, string destinationPath) { string shareLocation = Path.GetPathRoot(destinationPath); NetworkConnection.AddConnection(shareLocation, GlobalSettings.Items.DomainAdminCredential); try { // Copy the file if it does not already exist or is not already identical to source if (!File.Exists(destinationPath) || !FilesAreEqual(sourcePath, destinationPath)) { File.Copy(sourcePath, destinationPath, true); } } finally { NetworkConnection.RemoveConnection(shareLocation, forceRemoval: true); } }
private bool ValidateCredentials() { bool successful = false; try { string shareName = Path.GetPathRoot(ShareLocation); NetworkCredential credential = new NetworkCredential(userName_TextBox.Text, password_TextBox.Text, domain_TextBox.Text); // Attempt to connect to share NetworkConnection.AddConnection(shareName, credential); successful = true; } catch (Win32Exception ex) { MessageBox.Show(this, "Failed to connect to {0}: {1}".FormatWith(ShareLocation, ex.Message), this.Text); } return(successful); }
/// <summary> /// Deletes the file. /// </summary> /// <param name="sourcePath">The source path.</param> public static void DeleteFile(string sourcePath) { string shareLocation = Path.GetPathRoot(sourcePath); // The file exists on a server. Connect to that server first. NetworkConnection.AddConnection(shareLocation, GlobalSettings.Items.DomainAdminCredential); try { // We ignore files that do not exist. if (File.Exists(sourcePath)) { // Remove Readonly attribute new FileInfo(sourcePath).Attributes &= ~FileAttributes.ReadOnly; File.Delete(sourcePath); } } finally { NetworkConnection.RemoveConnection(shareLocation, forceRemoval: true); } }
/// <summary> /// Reads the file. /// </summary> /// <param name="sourcePath">The source path.</param> /// <returns></returns> public static string ReadFile(string sourcePath) { string text = string.Empty; string shareLocation = Path.GetPathRoot(sourcePath); // The file exists on a server. Connect to that server first. NetworkConnection.AddConnection(shareLocation, GlobalSettings.Items.DomainAdminCredential); try { // We ignore files that do not exist. if (File.Exists(sourcePath)) { text = File.ReadAllText(sourcePath); } } finally { NetworkConnection.RemoveConnection(shareLocation, forceRemoval: true); } return(text); }
/// <summary> /// Adds a collection of <see cref="PrintDeviceDriverCollection"/> to the testing environment. /// </summary> /// <param name="drivers">Collection of print driver packages to add to the testing environment.</param> /// <param name="overwrite">Specifies whether to overwrite the driver packages, should duplications be found. /// The default is false.</param> /// <returns> /// A new collection of print driver packages with INF file locations updated. /// </returns> public static void AddToFrameworkRepository(Collection <PrintDeviceDriverCollection> drivers, bool overwrite = false) { if (drivers == null) { throw new ArgumentNullException("drivers"); } string baseLocation = GlobalSettings.Items[Setting.PrintDriverServer]; NetworkCredential admin = GlobalSettings.Items.DomainAdminCredential; NetworkConnection.AddConnection(baseLocation, admin); try { foreach (PrintDeviceDriverCollection driver in drivers) { driver.AddToFrameworkRepository(baseLocation, overwrite); } } finally { NetworkConnection.RemoveConnection(baseLocation, forceRemoval: true); } }