您可以使用 C# 中的 System.IO.File 类来实现将本地文件复制到远程服务器。以下是一个示例代码,演示了如何使用 C# 进行文件复制:

using System;
using System.IO;
using System.Runtime.InteropServices;

class Program
{
    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword,
        int dwLogonType, int dwLogonProvider, out IntPtr phToken);

    static void Main()
    {
        string sourceFilePath = "C:\\path\\to\\local\\file.txt";
        string destinationFilePath = "\\172.16.199.95\\share\\file.txt";

        IntPtr adminToken;
        bool result = LogonUser("administrator", "172.16.199.95", "shkeboda456&*(", 9, 0, out adminToken);
        if (result)
        {
            try
            {
                using (WindowsIdentity.Impersonate(adminToken))
                {
                    File.Copy(sourceFilePath, destinationFilePath, true);
                    Console.WriteLine("File copied successfully.");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error occurred while copying file: " + e.Message);
            }
            finally
            {
                CloseHandle(adminToken);
            }
        }
        else
        {
            Console.WriteLine("LogonUser failed with error code: " + Marshal.GetLastWin32Error());
        }
    }

    [DllImport("kernel32.dll")]
    public static extern bool CloseHandle(IntPtr hObject);
}

请注意,上述代码中的'destinationFilePath'应该是远程服务器上的共享文件夹路径,以及您可能需要根据您的实际情况对源文件和目标文件进行更改。同时,确保您的程序具有足够的权限来访问和复制文件。

C# 使用 LogonUser() 复制本地文件到远程服务器

原文地址: https://www.cveoy.top/t/topic/b30C 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录