c#.net 上传文件到网络上的共享文件夹里面

yin7huang 2010-08-31 01:43:29
c#.net做上传文件,但不是上传到本地电脑上,是上传到其他电脑的共享文件夹里面
比如上传到 \\137.40.128.20\f001$\测试\测试共享 该怎么实现呢?
如果那个共享文件夹还是需要密码的 怎么实现?
...全文
2667 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
luoye12l 2011-03-18
  • 打赏
  • 举报
回复
权限不允许这样做,也不会把文件服务器的帐号、密码给你。那样的话,你直接就可以登录文件服务器了(中间可能会有域相隔,但也是不会把帐号之类的信息告诉给你的)
天地英豪 2010-10-24
  • 打赏
  • 举报
回复
up,两个牛人.
wuyq11 2010-08-31
  • 打赏
  • 举报
回复
<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);
}

}
孟子E章 2010-08-31
  • 打赏
  • 举报
回复
将文件上传到网络共享服务器的方法
详细参考
http://dotnet.aspx.cc/file/Upload-Files-TO-UNC-Share-Using-ASP.NET.aspx

1,在文件服务器上,创建一个本地帐户,比如登录名: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进行访问
孟子E章 2010-08-31
  • 打赏
  • 举报
回复
你可以参考
http://aspalliance.com/336_Upload_Files_Using_ASPNET_Impersonation_and_UNC_Share.all
孟子E章 2010-08-31
  • 打赏
  • 举报
回复
建立磁盘映射为本地盘符,如H:\
采用System.IO进行操作。

由于asp.net是匿名帐号运行,默认是没有权限的。保存之前需要进行代码模拟登录

62,066

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧