如何使用管理员权限为所有用户配置文件复制和替换文件

时间:2019-02-27 19:52:45

标签: c# windows authentication active-directory gpo

我已经在这里和Google上研究了所有文章。尽管有些似乎是我需要的,但我仍然遇到这个问题。我当然可以使用一些帮助来彻底解决此问题。

我想从服务器复制Customize.xml文件,并用它替换所有用户配置文件中的当前文件。我希望这样做,以便每次登录的人都可以运行。任何帮助弄清这一点并将其付诸实践的帮助将不胜感激。

System.UnauthorizedAccessException

using System;
using System.Configuration;
using System.IO;

namespace copy_delete_move_files
{
    public class SimpleFileCopy
    {
        public static object Logger { get; private set; }

        static void Main()
        {

            string fileName = "Customize.xml";
            string sourcePath = @"\\serverpath\c$\TestFolder";
            string targetPath = @"\\desktoppath\c$\%USERPROFILE%\APPDATA\Roaming\Litera\Customize";

            // Use Path class to manipulate file and directory paths.
            string sourceFile = Path.Combine(sourcePath, fileName);

            string destFile = Path.Combine(targetPath, fileName);

            // To copy a folder's contents to a new location:
            // Create a new target folder, if necessary.
            if (!Directory.Exists(targetPath))


            {
                Directory.CreateDirectory(targetPath);
            }

            // To copy a file to another location and 
            // overwrite the destination file if it already exists.
            File.Copy(sourceFile, destFile, true);

            // To copy all the files in one directory to another directory.
            // Get the files in the source folder.
            // Note: Check for target path was performed previously
            // in this code example.

            if (Directory.Exists(sourcePath))
            {
                string[] files = Directory.GetFiles(sourcePath);

                // Copy the files and overwrite destination files if they already exist.
                foreach (string s in files)
                {
                    // Use static Path methods to extract only the file name from the path.
                    fileName = Path.GetFileName(s);
                    destFile = Path.Combine(targetPath, fileName);
                    File.Copy(s, destFile, true);
                }
            }
            else
            {
                Console.WriteLine("Source path does not exist!");
            }

            // Keep console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
}

0 个答案:

没有答案