c# 通过文件夹共享复制文件到服务器
public static string[] GetFileNames(string directoryPath, string searchName)
{
return Directory.GetFiles(directoryPath, searchName, SearchOption.AllDirectories);
}
public class NetFileConntion
{
public static bool ConnectState(string path)
{
return ConnectState(path, "", "");
} public static bool ConnectState(string path, string userName, string passWord)
{
bool flag = false;
Process proc = new Process();
try
{
proc.StartInfo.FileName = " cmd.exe ";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
string dosLine = @"net use * /del /y
net use " + path + " /User:" + userName + " " + passWord + " /PERSISTENT:YES";
proc.StandardInput.WriteLine(dosLine);
proc.StandardInput.WriteLine(" exit ");
while (!proc.HasExited)
{
proc.WaitForExit();
}
string errormsg = proc.StandardError.ReadToEnd();
proc.StandardError.Close();
if (string.IsNullOrEmpty(errormsg))
{
flag = true;
}
else
{
throw new Exception(errormsg);
}
}
finally
{
proc.Close();
proc.Dispose();
}
return flag;
}
}
/// <summary>
/// 数据处理
/// </summary>
public override void Execute(JobExcutePara para)
{
if (ConfigHelper.Settings("RemoteCopyFileSwitch") != "")
{
return;
} //读取所有文件
var files = DirFileHelper.GetFileNames(ExcelRootPath, "*.*").Where(t => t.Contains("\\U_")).ToList();
//远程地址
var remotepaths = ConfigHelper.Settings("RemoteRootPaths")
.Split(new[] {'|'}, StringSplitOptions.RemoveEmptyEntries);
var remoteusers = ConfigHelper.Settings("RemoteUserPass")
.Split(new[] {'|'}, StringSplitOptions.RemoveEmptyEntries);
var sr=new StringBuilder();
foreach (var remotepath in remotepaths)
{
sr.Append(remotepath + ",");
try
{
bool status = NetFileConntion.ConnectState(remotepath, remoteusers[], remoteusers[]);
sr.Append(status + "," + files.Count + "," + remoteusers[] + "," + remoteusers[]);
if (status)
{
var startTime = new DateTime(DateTime.Now.Year,
DateTime.Now.Month,
DateTime.Now.Day,
, , );
//读取目标目录文件
var theFiles = DirFileHelper.GetFileNames(remotepath, "*.*");
sr.Append(theFiles.Length + ","); foreach (var file1 in files)
{
var fileinfo_1=new FileInfo(file1);
if (fileinfo_1.LastWriteTime < startTime)
{
continue;
}
var filepath = file1.Replace(ExcelRootPath, string.Empty);
var flag = false;
foreach (var file2 in theFiles)
{
//检查文件是否存在
if (file2.Contains(filepath))
{
flag = true;
break;
}
}
if (!flag)
{
//没有则进行写入复制
var targetPath = remotepath + filepath;
if (!Directory.Exists(Path.GetDirectoryName(targetPath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(targetPath));
}
File.Copy(file1, targetPath);
}
}
}
}
catch (Exception ex)
{
LogHelper._Error("JobExcute__CopyFile ERROR", ex);
}
}
//LogHelper._Info(sr.ToString());
}
c# 通过文件夹共享复制文件到服务器的更多相关文章
- java-FileUtils(复制文件夹、复制文件、字符串直接写入文件中)(新手)
实例: lx1: import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; pu ...
- C# 把一个文件夹下所有文件复制到另一个文件夹下 把一个文件夹下所有文件删除(转)
C# 把一个文件夹下所有文件复制到另一个文件夹下 public static void CopyDirectory(string srcPath, string destPath) { try { ...
- VMWare12虚拟机实现主客机间的文件拖拽(复制粘贴)和文件夹共享
版本: 主机:Windows 7 64位旗舰版 虚拟机: VMWare 12 + Windows 7 64位旗舰版 VMWare pro 12 + Ubuntu16.04LTS 64位 注:由于VMW ...
- 解决WIN7与虚拟机CentOS的文件夹共享问题
一.系统与软件 WIN7 64bit.VirtualBox 5.0.14.CentOS 6.5.SecureCRT 7.2.3 二.使用文件夹共享需要安装增强功能,但是安装时无法读取光盘iso文件 三 ...
- 负载均衡下的资源文件配置/多站点下的资源文件夹共享(Windows IIS)
前言: 负载均衡用的是NLB,微软的方案不太靠谱,举个例子吧,AB两台服务器负载出C,如果用户访问访问C之后分配的是A,那么如果A挂了,是不会自动切换到B的.据说后来还有一种NLB的方案可以实现,也不 ...
- win7文件夹共享(不新建账户、不带密码直接访问)
1.右键需要共享的文件夹>共享>特定用户>选择Everyone>“添加”>“共享” 2.打开控制面板>按类别的查看方式>网络和Internet下的选择家庭组和 ...
- Powershell学习之道-文件夹共享及磁盘映射
导读 在Linux环境下,我们很轻易就能得心应手地通过命令操作一切事物,在Windows下,Powershell也算是后起之秀,提供大量的cmdlet以及c#的横向拓展.下面将由小编带领大家通过Pow ...
- linux下跨服务器文件文件夹的复制
文件的复制:scp –P (端口号) ./authorized_keys berchina@hadoop002:/home/berchina 文件夹的复制:scp -r -P (端口号) /home/ ...
- MFC下对文件及文件夹的操作(复制、剪切、删除、创建文件夹,写文件)
一.文件夹的创建 void CFileOperationDlg::OnButtonMakeFolder() { // TODO: Add your control notification handl ...
随机推荐
- iOS邮箱、手机号等常用验证功能 判断字符串是否int float
手机号验证 /* 手机号码 13[0-9],14[5|7|9],15[0-3],15[5-9],17[0|1|3|5|6|8],18[0-9] 移动:134[0-8],13[5-9],147,15[0 ...
- unittest框架(一)用例管理
在unittest框架的自动化接口测试中,可以用yaml文件来管理用例,这样一方面,不会像excel管理用例那么死板:另一方面,数据读取出来就是一个字典,便于取值,用起来更灵活. 首先,需要安装一个模 ...
- 1:1 Struts2概述
jar包下载
- python的构建工具setup.py
一.构建工具setup.py的应用场景 在安装python的相关模块和库时,我们一般使用“pip install 模块名”或者“python setup.py install”,前者是在线安装,会安 ...
- Mybatis 之动态代理
使用Mybatis 开发Web 工程时,通过Mapper 动态代理机制,可以只编写接口以及方法的定义. 如下: 定义db.properties driver=oracle.jdbc.OracleDri ...
- react headtop title 截取
render() { const nav = this.props.nav const text = nav && nav.length > 5 ? this.strHandle ...
- 20145204《Java程序设计》第10周学习总结
网络编程 网络编程:在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是把数据发送到指定的位置,或者接收到指定的数据,这个就是狭义的网络编程范畴.在发送和接收数据时,大部分的程序设计 ...
- Java Quartz用法
code: 这里的MyJob必须是public,这里Job实例化的时候要用到反射,必须是public的,不能与调度操作放同一个.java文件中. package com.qhong; import o ...
- Java8 Lambda
Demo: package com.qhong; public class Main { public static void main(String[] args) throws Exception ...
- ASP.NET WEB API 2: HTTP MESSAGE LIFECYLE
https://www.asp.net/media/4071077/aspnet-web-api-poster.pdf 1.You can host Web API inside IIS or ins ...