我想从服务器复制一个xml文件,然后连接到所有用户配置文件,并用从服务器复制的一个来覆盖他们拥有的用户配置文件xml文件。我希望它是一个可执行文件,因此可以与SCCM一起为所有用户运行。我知道必须有管理权限,但是我不确定如何编写它。对于如何以不同的方式进行操作,我也持开放态度,但我确实想使用C#进行操作,并将其制作为SCCM的可执行文件。
namespace copy_delete_move_files
{
public class SimpleFileCopy
{
public static object Logger { get; private set; }
static void Main()
{
string fileName = "Customize.xml";
string sourcePath = @"\\pathToServer\c$\TestFolder";
string targetPath = @"\\pathToUserProfiles\c$\%USERPROFILE%\APPDATA\Roaming\Folder\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 :(得分:1)
使用SCCM,您可以通过以下步骤实现此目标:
以适合一个用户的方式创建程序(整个多用户部分将留给sccm,您只需以双击该用户的方式对其进行编写即可)。作为appdata路径,您只需要使用以下内容:
string fileNameWithExt = "your folderpath and filename";
string destPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), fileNameWithExt);
在SCCM中,当您在向导中创建程序时,选择“程序可以运行:仅当用户登录时才能运行”和“运行模式:以用户权限运行”。创建完成后,您输入程序的属性,然后转到“高级”,然后选择“将程序分配给计算机时:对每个登录的用户运行一次”
这确保不仅现在所有用户而且将来所有用户都可以获取该程序。如果您确实要从服务器进行复制,这也可以为您提供帮助,因为默认情况下,SCCM使用本地系统帐户,该帐户对服务器共享没有访问权限(尽管您可以授予它们)。应用程序而不是程序也应该可以。
缺点是: