Copy files to a folder which need have Administrator approve and overwrite the existing same readonly files

1. Program.cs

using System;

using System.IO;

namespace CopyUnitTestDriver

{

classProgram

{

staticvoid Main(string[] args)

{

string destinationPath = @"C:\Program Files (x86)\Microsoft Visual Studio 11.0\Blend\Test\";

string sourcePath = @"\\authoring\Public\User\v-lingc\blendRun\UnitTestDriver";

string[] filePath = newstring[] { destinationPath + @"UnitTestDriver.exe", destinationPath + @"UnitTestDriver.exe.config", destinationPath + @"UnitTestDriver.exe.vspscc", destinationPath + @"UnitTestDriver.pdb" };

FileInfo[] oldFiles = newFileInfo[] { newFileInfo(filePath[0]), newFileInfo(filePath[1]), newFileInfo(filePath[2]), newFileInfo(filePath[3]) };

foreach (string file in filePath)

{

FileAttributes attributes = File.GetAttributes(file);

if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)

{

attributes = RemoveAttribute(attributes, FileAttributes.ReadOnly);

File.SetAttributes(file, attributes);

}

}

foreach (FileInfo file in oldFiles)

{

file.Delete();

}

DirectoryInfo info = newDirectoryInfo(sourcePath);

foreach (FileSystemInfo fsi in info.GetFileSystemInfos())

{

String destName = Path.Combine(destinationPath, fsi.Name);

File.Copy(fsi.FullName, destName, true);

}

}

privatestaticFileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove)

{

return attributes & ~attributesToRemove;

}

}

}

 2. Add a new item Application Manifest File:

<?xmlversion="1.0"encoding="utf-8"?>

<asmv1:assemblymanifestVersion="1.0"xmlns="urn:schemas-microsoft-com:asm.v1"xmlns:asmv1="urn:schemas-microsoft-com:asm.v1"xmlns:asmv2="urn:schemas-microsoft-com:asm.v2"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

  <assemblyIdentityversion="1.0.0.0"name="MyApplication.app"/>

  <trustInfoxmlns="urn:schemas-microsoft-com:asm.v2">

    <security>

      <requestedPrivilegesxmlns="urn:schemas-microsoft-com:asm.v3">

        <requestedExecutionLevellevel="highestAvailable"uiAccess="false" />

      </requestedPrivileges>

    </security>

  </trustInfo>

  <compatibilityxmlns="urn:schemas-microsoft-com:compatibility.v1">

    <application>

    </application>

  </compatibility>

</asmv1:assembly>

原文地址:https://www.cnblogs.com/LilianChen/p/2922458.html