How to copy and replace a file for all user profiles with Admin rights

笑着哭i 提交于 2019-12-25 00:29:51

问题


I have researched all the articles here and on Google. While some seem to be what I need I continue to run into this problem. I could certainly use some assistance getting this resolved once and for all.

I want to copy a Customize.xml file from the server and have it replace the current file in all user profiles. I would prefer to make this so that it will run for anyone that logs on each time. Any assistance figuring this out and getting it to work would be greatly appreciated.

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();
        }
    }
}

来源:https://stackoverflow.com/questions/54913456/how-to-copy-and-replace-a-file-for-all-user-profiles-with-admin-rights

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!