服务器共享文件登陆,.net c# 服务器共享文件夹 windows远程登录 代码

一个刚刚开始学习编程的人,若是遇到问题没法解决可能会寻找别的解决方案,若是久而久之可能会放弃这门语言而学习其余的语言...编程

开源与分享的重要性api

使用场景:将网站全部附件上传到指定服务器的共享目录下,首先在服务器上新建文件夹并共享服务器

1. 新建类 FileHelperide

namespace Frame.FileHelper

{学习

public class FileHelper

{网站

public static void CreateFolder(string path)

{

string dirPath = path.Substring(0, path.LastIndexOf("\\"));

if (!Directory.Exists(dirPath))

{

Directory.CreateDirectory(dirPath);

}spa

}orm

}token

public class FileConnect : IDisposable

{rem

#region win32 API

[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, 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;

WindowsIdentity newIdentity;

WindowsImpersonationContext impersonatedUser;

bool isSuccess = false;

IntPtr token = IntPtr.Zero;

public bool IsConnectted

{

get { return isSuccess; }

}

public FileConnect()

{

string fileServer = ConfigurationManager.AppSettings["FileServer"];  //远程服务器地址

if (!string.IsNullOrEmpty(fileServer))

{

isSuccess = Connect(fileServer, ConfigurationManager.AppSettings["FileServerUserName"], ConfigurationManager.AppSettings["FileServerPassword"]); //登陆名 密码

}

else

{

isSuccess = true;

}

}

public bool Connect(string remoteAddr, string userName, string password)

{

bool isSuc = false;

try

{

isSuc = LogonUser(userName,remoteAddr, password, LOGON32_LOGON_NEW_CREDENTIALS, LOGON32_PROVIDER_DEFAULT, ref token);

newIdentity = new WindowsIdentity(token);

impersonatedUser = newIdentity.Impersonate();

}

catch (Exception )

{

return false;

}

return isSuc;

}

public void DisConnect()

{

if (isSuccess)

{

if (impersonatedUser != null)

impersonatedUser.Undo();

if (token != IntPtr.Zero)

CloseHandle(token);

}

}

public void Dispose()

{

DisConnect();

}

#endregion

}

}

---------------------------------------------

2.上传

public string uploadFileAndThumb(HttpContext context, string paramData, params object[] param)

{

using (FileConnect connect = new FileConnect())

{

string str_Result = "";

if (!connect.IsConnectted)

{

// throw new UException("");

str_Result = "{status:'fail',info:'文件服务器配置错误!'}";

return str_Result;

}

HttpPostedFile file = context.Request.Files["FileData"];

string extensionName = string.Empty;

string fileName = string.Empty;

if (file.FileName.LastIndexOf(".") > 0)

{

int len = file.FileName.LastIndexOf(".");

extensionName = file.FileName.Substring(len);

int lastIndex = file.FileName.LastIndexOf("\\");

if (lastIndex>0)

{

int subLength = len - 1 - lastIndex;

fileName = file.FileName.Substring(lastIndex+1, subLength);

}

else

{

//chorme

fileName = file.FileName.Substring(0, file.FileName.LastIndexOf("."));

}

}

string date = DateTime.Now.ToString("yyyyMMdd");

string pID="";

string relativePath = string.Format(@"\Project\{0}\{1}\", pID, date);

string FileFolder = System.Configuration.ConfigurationManager.AppSettings["FilePath"]; // 相似 \\10.2.183.21

string absoluteName = FileFolder + relativePath+ fileName+extensionName;

FileHelper.CreateFolder(absoluteName);

//判断文件大小

long length = file.ContentLength;

if (length > Convert.ToInt32(param[5]))

{

return "{ status:'100100',info:'' }";

}

else

{

file.SaveAs(absoluteName);

}

}

}

-------------------------------------------

3. 在须要下载附件 的页面 引用如下代码,登陆远程服务器

//登陆共享文件夹

using (FileConnect coneect = new FileConnect())

{

if (!coneect.IsConnectted)

{

// throw new UException("文件服务器配置错误");

}

}

--------------------------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值