c#.net 上传文件到网络上的共享文件夹里面 c#.net做上传文件,但不是上传到本地电脑上,是上传到其他电脑的共享文件夹里面比如上传到 \\137.40.128.20\f001$\测试\测试共享 该怎么实现呢? 如果那个共享文件夹还是需要密码的 怎么实现? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 建立磁盘映射为本地盘符,如H:\采用System.IO进行操作。由于asp.net是匿名帐号运行,默认是没有权限的。保存之前需要进行代码模拟登录 你可以参考http://aspalliance.com/336_Upload_Files_Using_ASPNET_Impersonation_and_UNC_Share.all 将文件上传到网络共享服务器的方法详细参考http://dotnet.aspx.cc/file/Upload-Files-TO-UNC-Share-Using-ASP.NET.aspx1,在文件服务器上,创建一个本地帐户,比如登录名:upload,密码:upload,注意在创建的时候选择“密码永不过期”,去掉勾选“用户下次登录时须更改密码”的选项;2,在要共享的文件夹上点右键,选择“属性”-“安全”,增加upload帐户可以写入的权限;3,在要共享的文件夹上点右键,选择“共享”,共享此文件夹,并在“权限”按钮点击后添加帐户upload可修改;4,在另外一台 Web 服务器上,创建登录名和密码与上面完全相同的本地帐户。5,在web.config里,启用模拟: web.config里添加的代码<identity impersonate="true" userName="upload" password="upload" />6,在网站文件夹和Temporary ASP.NET Files文件夹上设置帐户upload读写权限7,在ASP.NET的上传文件里写:C# 代码protected void Button1_Click(object sender, EventArgs e){ string fileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName); FileUpload1.SaveAs(@"\\192.168.3.1\free\" + fileName);}8,显示上传的文件:在IIS里创建虚拟目录,指向“另一台计算机上的共享”,“连接为”输入上面创建的帐户名和密码。即可以http://www.mengxianhui.com/upload/hello.jpg进行访问 <system.web> <identity impersonate="true" userName="User01" password="对应User01的密码" /> </system.web> 然后A服务器和B服务器上都创建该用户public class NetFileSave { [DllImport("advapi32.dll", SetLastError = true)] private static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken); [DllImport("kernel32.dll", CharSet = CharSet.Auto)] private unsafe static extern int FormatMessage(int dwFlags, ref IntPtr lpSource, int dwMessageId, int dwLanguageId, ref String lpBuffer, int nSize, IntPtr* arguments); [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern bool CloseHandle(IntPtr handle); [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] public extern static bool DuplicateToken(IntPtr existingTokenHandle, int SECURITY_IMPERSONATION_LEVEL, ref IntPtr duplicateTokenHandle); // logon types const int LOGON32_LOGON_INTERACTIVE = 2; const int LOGON32_LOGON_NETWORK = 3; const int LOGON32_LOGON_NEW_CREDENTIALS = 9; // logon providers const int LOGON32_PROVIDER_DEFAULT = 0; const int LOGON32_PROVIDER_WINNT50 = 3; const int LOGON32_PROVIDER_WINNT40 = 2; const int LOGON32_PROVIDER_WINNT35 = 1; [STAThread] public static bool FileSave(string strServer,string strAccount,string strPassword,string strFileFromPath,string strFileToPath) { IntPtr token = IntPtr.Zero; IntPtr dupToken = IntPtr.Zero; bool isSuccess = LogonUser(strAccount, strServer, strPassword, LOGON32_LOGON_NEW_CREDENTIALS, LOGON32_PROVIDER_DEFAULT, ref token); if (!isSuccess) { RaiseLastError(); return false; } isSuccess = DuplicateToken(token, 2, ref dupToken); if (!isSuccess) { RaiseLastError(); } WindowsIdentity newIdentity = new WindowsIdentity(dupToken); WindowsImpersonationContext impersonatedUser = newIdentity.Impersonate(); try { FileInfo srcFile = new FileInfo(strFileFromPath); srcFile.MoveTo(strFileToPath); } catch { return false; } impersonatedUser.Undo(); isSuccess = CloseHandle(token); if (!isSuccess) { RaiseLastError(); return false; } return true; } public unsafe static string GetErrorMessage(int errorCode) { int FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100; int FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200; int FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000; int messageSize = 255; string lpMsgBuf = ""; int dwFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS; IntPtr ptrlpSource = IntPtr.Zero; IntPtr ptrArguments = IntPtr.Zero; int retVal = FormatMessage(dwFlags, ref ptrlpSource, errorCode, 0, ref lpMsgBuf, messageSize, &ptrArguments); if (retVal == 0) { throw new ApplicationException( string.Format("Failed to format message for error code '{0}'.", errorCode)); } return lpMsgBuf; } private static void RaiseLastError() { int errorCode = Marshal.GetLastWin32Error(); string errorMessage = GetErrorMessage(errorCode); throw new ApplicationException(errorMessage); } } 统计问题 问一个Button1_Click()的问题? 有关AjaxPanel控件的问题,请教高手 超级链接的下划线问题 请问,如何关闭asp.net程序出错时记录系统日志 两个自定义控件的互相访问!80分顶有分! 窗体的身份验证Web.config 中配置设置验证过期的问题? 100分求助!超链接问题如何捕获网页上的点击事件 jq 两个ajax不能同时运行的问题 mvc中handler怎么使用 怎么得到父级容器 asp.net网站运行时如何只打开一个页面
采用System.IO进行操作。由于asp.net是匿名帐号运行,默认是没有权限的。保存之前需要进行代码模拟登录
http://aspalliance.com/336_Upload_Files_Using_ASPNET_Impersonation_and_UNC_Share.all
详细参考
http://dotnet.aspx.cc/file/Upload-Files-TO-UNC-Share-Using-ASP.NET.aspx1,在文件服务器上,创建一个本地帐户,比如登录名:upload,密码:upload,注意在创建的时候选择“密码永不过期”,去掉勾选“用户下次登录时须更改密码”的选项;
2,在要共享的文件夹上点右键,选择“属性”-“安全”,增加upload帐户可以写入的权限;
3,在要共享的文件夹上点右键,选择“共享”,共享此文件夹,并在“权限”按钮点击后添加帐户upload可修改;
4,在另外一台 Web 服务器上,创建登录名和密码与上面完全相同的本地帐户。
5,在web.config里,启用模拟:
web.config里添加的代码
<identity impersonate="true" userName="upload" password="upload" />
6,在网站文件夹和Temporary ASP.NET Files文件夹上设置帐户upload读写权限
7,在ASP.NET的上传文件里写:
C# 代码
protected void Button1_Click(object sender, EventArgs e)
{
string fileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(@"\\192.168.3.1\free\" + fileName);
}8,显示上传的文件:
在IIS里创建虚拟目录,指向“另一台计算机上的共享”,“连接为”输入上面创建的帐户名和密码。即可以http://www.mengxianhui.com/upload/hello.jpg进行访问
<identity impersonate="true" userName="User01" password="对应User01的密码" />
</system.web>
然后A服务器和B服务器上都创建该用户
public class NetFileSave
{
[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken); [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private unsafe static extern int FormatMessage(int dwFlags, ref IntPtr lpSource, int dwMessageId, int dwLanguageId, ref String lpBuffer, int nSize, IntPtr* arguments); [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool CloseHandle(IntPtr handle); [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public extern static bool DuplicateToken(IntPtr existingTokenHandle, int SECURITY_IMPERSONATION_LEVEL, ref IntPtr duplicateTokenHandle); // logon types
const int LOGON32_LOGON_INTERACTIVE = 2;
const int LOGON32_LOGON_NETWORK = 3;
const int LOGON32_LOGON_NEW_CREDENTIALS = 9; // logon providers
const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_PROVIDER_WINNT50 = 3;
const int LOGON32_PROVIDER_WINNT40 = 2;
const int LOGON32_PROVIDER_WINNT35 = 1;
[STAThread]
public static bool FileSave(string strServer,string strAccount,string strPassword,string strFileFromPath,string strFileToPath)
{
IntPtr token = IntPtr.Zero;
IntPtr dupToken = IntPtr.Zero; bool isSuccess = LogonUser(strAccount, strServer, strPassword, LOGON32_LOGON_NEW_CREDENTIALS, LOGON32_PROVIDER_DEFAULT, ref token);
if (!isSuccess)
{
RaiseLastError();
return false;
} isSuccess = DuplicateToken(token, 2, ref dupToken);
if (!isSuccess)
{
RaiseLastError();
} WindowsIdentity newIdentity = new WindowsIdentity(dupToken);
WindowsImpersonationContext impersonatedUser = newIdentity.Impersonate(); try
{
FileInfo srcFile = new FileInfo(strFileFromPath);
srcFile.MoveTo(strFileToPath);
}
catch
{
return false;
} impersonatedUser.Undo(); isSuccess = CloseHandle(token);
if (!isSuccess)
{
RaiseLastError();
return false;
} return true;
} public unsafe static string GetErrorMessage(int errorCode)
{
int FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100;
int FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200;
int FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000; int messageSize = 255;
string lpMsgBuf = "";
int dwFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS; IntPtr ptrlpSource = IntPtr.Zero;
IntPtr ptrArguments = IntPtr.Zero; int retVal = FormatMessage(dwFlags, ref ptrlpSource, errorCode, 0, ref lpMsgBuf, messageSize, &ptrArguments);
if (retVal == 0)
{
throw new ApplicationException(
string.Format("Failed to format message for error code '{0}'.", errorCode));
} return lpMsgBuf;
} private static void RaiseLastError()
{
int errorCode = Marshal.GetLastWin32Error();
string errorMessage = GetErrorMessage(errorCode); throw new ApplicationException(errorMessage);
}
}