这是引用的命名空间(有一部分是不需要的,可以自行去掉)
using Oracle.DataAccess.Client;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Text;
using EMPPLib;
using System.Threading;
using log4net;
using System.IO;
using System.Net;
using System.Web;
using System.Runtime.InteropServices;
string HttpDownloadFile(string url, string path, string path2)
{
try
{
//获取文件目录,若不存在则创建
string date = DateTime.Now.ToString("yyyyMMdd");
path2 += date;
if (!Directory.Exists(path2))
{
Directory.CreateDirectory(path2);
}
string fileName = Path.GetFileName(url); // 获取源文件名
string timestamp = DateTime.Now.ToString("yyyyMMddHHmmss"); // 生成时间戳
newName = $"{Path.GetFileNameWithoutExtension(fileName)}_{timestamp}{Path.GetExtension(fileName)}"; // 拼接新文件名
string destPath = Path.Combine(path2, newName); // 目标文件路径
string WLFAS_USER = System.Configuration.ConfigurationManager.AppSettings["WLFAS_USER"].ToString();
string WLFAS_PASS = System.Configuration.ConfigurationManager.AppSettings["WLFAS_PASS"].ToString();
string WLFAS_IP = System.Configuration.ConfigurationManager.AppSettings["WLFAS_IP"].ToString();
try
{
using (SharedTool tool = new SharedTool(WLFAS_USER, WLFAS_PASS, WLFAS_IP))
{
if (File.Exists(url))
{
// 打开共享文件流以进行读取
using (FileStream sharedFileStream = File.OpenRead(url))
{
// 创建目标文件流以进行写入
using (FileStream destinationFileStream = File.Create(destPath))
{
// 使用BinaryReader从共享文件中读取内容
using (BinaryReader reader = new BinaryReader(sharedFileStream))
{
// 使用BinaryWriter将内容写入目标文件
using (BinaryWriter writer = new BinaryWriter(destinationFileStream))
{
byte[] buffer = new byte[1080];
int bytesRead;
while ((bytesRead = reader.Read(buffer, 0, buffer.Length)) > 0)
{
writer.Write(buffer, 0, bytesRead);
}
}
}
}
}
}
else
{
logger.Error("无法访问共享文件夹");
}
}
}
catch (Exception ex)
{
logger.Error("访问共享文件夹时发生错误: \t" + ex.Message);
}
return destPath;
}
catch (Exception ex)
{
logger.Error("源文件地址: \t" + url);
logger.Error("目标文件地址:\t" + path);
logger.Error("异常类型: \t" + ex.GetType());
logger.Error("异常描述:\t" + ex.Message);
logger.Error("异常方法:\t" + ex.TargetSite);
logger.Error("异常堆栈:\t" + ex.StackTrace);
return null;
}
}
public class SharedTool : IDisposable
{
// obtains user token
[DllImport("advapi32.dll", SetLastError = true)]
static extern bool LogonUser(string pszUsername, string pszDomain, string pszPassword,
int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
// closes open handes returned by LogonUser
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
extern static bool CloseHandle(IntPtr handle);
[DllImport("Advapi32.DLL")]
static extern bool ImpersonateLoggedOnUser(IntPtr hToken);
[DllImport("Advapi32.DLL")]
static extern bool RevertToSelf();
const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_LOGON_NEWCREDENTIALS = 9;//域控中的需要用:Interactive = 2
private bool disposed;
public SharedTool(string username, string password, string ip)
{
// initialize tokens
IntPtr pExistingTokenHandle = new IntPtr(0);
IntPtr pDuplicateTokenHandle = new IntPtr(0);
try
{
// get handle to token
bool bImpersonated = LogonUser(username, ip, password,
LOGON32_LOGON_NEWCREDENTIALS, LOGON32_PROVIDER_DEFAULT, ref pExistingTokenHandle);
if (bImpersonated)
{
if (!ImpersonateLoggedOnUser(pExistingTokenHandle))
{
int nErrorCode = Marshal.GetLastWin32Error();
throw new Exception("ImpersonateLoggedOnUser error;Code=" + nErrorCode);
}
}
else
{
int nErrorCode = Marshal.GetLastWin32Error();
throw new Exception("LogonUser error;Code=" + nErrorCode);
}
}
finally
{
// close handle(s)
if (pExistingTokenHandle != IntPtr.Zero)
CloseHandle(pExistingTokenHandle);
if (pDuplicateTokenHandle != IntPtr.Zero)
CloseHandle(pDuplicateTokenHandle);
}
}
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
RevertToSelf();
disposed = true;
}
}
public void Dispose()
{
Dispose(true);
}
}