.net c# 服务器共享文件夹 windows远程登陆 代码
一个刚刚开始学习编程的人,如果遇到问题无法解决可能会寻找别的解决方案,如果长此以往可能会放弃这门语言而学习其他的语言...
开源与分享的重要性
使用场景:将网站所有附件上传到指定服务器的共享目录下,首先在服务器上新建文件夹并共享
1. 新建类 FileHelper
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);
}
}
}
public class FileConnect : IDisposable
{
#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("文件服务器配置错误");
}
}
--------------------------------------------
.net c# 服务器共享文件夹 windows远程登陆 代码的更多相关文章
- 同一个局域网内,使用 java 从服务器共享文件夹中复制文件到本地。
1 引用jar 包 <dependency> <groupId>org.samba.jcifs</groupId> <artifactId>jcifs& ...
- virtualbox设置共享文件夹代替sftp同步代码
通常的开发场景: 代码放在virtualbox上运行,本地的IDE通过sftp实现和虚拟机的代码同步. 有 一个不能避免的问题是,当使用git时,如果装在virtualbox端,那么每次virtual ...
- Ubuntu云服务器下mysql授权远程登陆
1)首先以 root 帐户登陆 MySQL(在授权之前要确保3306端口开放)2)创建远程登陆用户并授权 > grant all PRIVILEGES on discuz.* to zhan@' ...
- 远程控制服务(SSH)之Windows远程登陆Linux主机
本篇blog同样介绍两种方式进行. 首先进行准备工作: 1.所用到的工具如下: (1) 装有Linux系统的VMware虚拟机*1 (2) 终端连接工具Xshell 6 2.将Wind ...
- win server2012 r2 服务器共享文件夹设置
按照普通的win7 设置共享文件夹,不起作用 于是乎倒腾,百度,总结以下步骤 1.启用guest账号 控制面板->用户账户->管理其他账户->Guest启用 2.设置共享文件夹 添加 ...
- VirtualBox共享文件夹 Windows 7 (宿主机) + Ubuntu 12.04
1 安装增强功能包1.1 运行Ubuntu并登陆,菜单“设备”->“安装增强功能包(Install Guest Additions)”ubun1.2 桌面上会多出一个光盘图标,光盘默认自动加载到 ...
- CentOS中设置Windows共享文件夹
在CentOS中设置Samba可实现和Windows共享文件夹.常见的需求:1)用户能够在Windows机器上通过共享文件夹访问远程Linux服务器上自己的主目录:2)用户能够在Windows机器上访 ...
- Linux服务器挂载windows共享文件夹和nas存储
需求: 公司有3.4T多的小文件需要copy到公司内部的nas存储中,由于小文件太多,数据量太大,整盘copy时速度极慢:只能人工对3.4T多的数据分批次的导入,这对于搞计算机的来说是不能忍受的,于是 ...
- Windows Azure Storage (20) 使用Azure File实现共享文件夹
<Windows Azure Platform 系列文章目录> Update 2016-4-14.在Azure VM配置FTP和IIS,请参考: http://blogs.iis.net/ ...
随机推荐
- windows安装mysql5.7
1下载mysql http://dev.mysql.com/downloads/mysql/2解压后,新建一个data文件夹,复制my-default.ini,并改名为my.ini,添加下面内容[cl ...
- CentOS7下搭建邮件服务器(dovecot + postfix + SSL)
CentOS 花了基本上两天的时间去配置CentOS7下的邮件服务器.其中艰辛太多了,一定得总结下. 本文的目的在于通过一系列配置,在CentOS 7下搭建dovecot + postfix + ...
- C#模拟鼠标键盘控制其他窗口(一)
编写程序模拟鼠标和键盘操作可以方便的实现你需要的功能,而不需要对方程序为你开放接口.比如,操作飞信定时发送短信等.我之前开发过飞信耗子,用的是对飞信协议进行抓包,然后分析协议,进而模拟协议的执行,开发 ...
- IIS连接数、IIS并发连接数、IIS最大并发工作线程数、应用程序池的队列长度、应用程序池的
IIS连接数 一般购买过虚拟主机的朋友都熟悉购买时,会限制IIS连接数,这边先从普通不懂代码用户角度理解IIS连接数 顾名思义即为IIS服务器可以同时容纳客户请求的最高连接数,准确的说应该叫" ...
- javascript基础02
javascript基础02 1.数据类型 数据类型的描述在上篇的扩展中有写到链接 由于ECMAScript数据类型具有动态性,因此的确没有再定义其他数据类型的必要.这句话很重要. 如果以后再数据类型 ...
- IIs管理服务一直启动失败的原因之一
首先eventlog里面的日志: 万维网发布服务(WWW 服务)没有为站点 1 注册 URL 前缀 https://*:8172/.该站点已被禁用.数据字段包含错误号. IISWMSVC_STARTU ...
- linux常用命令-用户管理命令
useradd 用户名 passwd 用户名:修改该用户的密码 groupadd 组名 who: 查看现在登录了几个用户,得到的信息含义如下 登录用户名 登录终端 登录时间 登录终端的IP地址(如果没 ...
- T-SQL 语句的优化
SQL调优. 1.索引是数据库调优的最根本的优化方法.聚簇索引.非聚簇索引. 聚簇索引:物理序与索引顺序相同.(只能有一个) 非聚簇索引:物理顺序与索引顺序不相同. 2.调整WHERE 子句中的连接顺 ...
- css3部分选择器整理
整理些选择器,加深印象和理解 标签选择器 body{} 表示body标签 类选择器 .className{} 表示类名class为className的所有标签 id选择器 #idName{} 表示id ...
- Effective C++ -----条款54:让自己熟悉包括TR1在内的标准程序库
C++ 标准程序库的主要机能由STL.iostream.locales 组成.并包含C99 标准程序库. TR1 添加了只能指针(例如 tr1::shared_ptr).一般化函数指针(tr1::fu ...