1 string pLocalFilePath ="";//要复制的文件路径 2 string pSaveFilePath ="";//指定存储的路径 3 if (File.Exists(pLocalFilePath))//必须判断要复制的文件是否存在 4 { 5 File.Copy(pLocalFilePath, pSaveFilePath, true);//三个参数分别是源文件路径,存储路径,若存储路径有相同文件是否替换 6 }
1 string pLocalFilePath ="";//要复制的文件路径 2 string pSaveFilePath ="";//指定存储的路径 3 if (File.Exists(pLocalFilePath))//必须判断要复制的文件是否存在 4 { 5 File.Copy(pLocalFilePath, pSaveFilePath, true);//三个参数分别是源文件路径,存储路径,若存储路径有相同文件是否替换 6 }
转载于:https://www.cnblogs.com/hahahayang/p/10292958.html
C# 复制文件及文件夹
/// <summary>
/// 复制文件夹及文件
/// </summary>
/// <param name="sourceFolder">原文件路径</param>
/// <param name="destFolder">目标文件路径</param>
/// <returns></returns>
public static int CopyFolder(string sourceFolder, string destFolder)
{
try
{
//如果目标路径不存在,则创建目标路径
if (!System.IO.Directory.Exists(destFolder))
{
System.IO.Directory.CreateDirectory(destFolder);
}
//得到原文件根目录下的所有文件
string[] files = System.IO.Directory.GetFiles(sourceFolder);
foreach (string file in files)
{
string name = System.IO.Path.GetFileName(file);
string dest = System.IO.Path.Combine(destFolder, name);
System.IO.File.Copy(file, dest);//复制文件
}
//得到原文件根目录下的所有文件夹
string[] folders = System.IO.Directory.GetDirectories(sourceFolder);
foreach (string folder in folders)
{
string name = System.IO.Path.GetFileName(folder);
string dest = System.IO.Path.Combine(destFolder, name);
CopyFolder(folder, dest);//构建目标路径,递归复制文件
}
return 1;
}
catch (Exception e)
{
return -1;
}
}
直接上代码!
public class ToolMoveFiles
{
public static bool MoveDirectory(string path, string docID)
{
// string dosLine = @"net use " + path + " /User:test \"123456\" /PERSISTENT:YES";
string dosLine = @"net use " + path + " /User:Guest \"\" /PERSISTENT:NO";
connect(dosLine);
//net use h: \\ip\folders password /user:域名\user
if (path.IndexOf(IpHelper.GetLocalIP().Trim())>0)
{
return true;
}
CopyFile(path, docID);
// return Flag;
return true;
}
public static string CopyFile(string form_path, string toPath)//, string dosLine
{
Process proc = new Process();
string cmd = ($"xcopy {form_path} {toPath} /y /e /i /q"); //xcopy \\10.122.55.4\websites\test E:\demo\test\ /D /E /Y /K
try
{
return cmd + " == " + Docopy(proc, cmd);
}
catch (Exception ex)
{
cleanConnect();
// connect(dosLine);
Docopy(proc, cmd);
return ex.Message;
}
// return cmd;
}
//cmd命令拷贝 xcopy
public static string Docopy(Process proc, string cmd)
{
proc.StartInfo.FileName = @"C:\Windows\System32\cmd.exe";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = true;//true表示不显示黑框,false表示显示dos界面
// proc.StartInfo.Arguments = $" {cmd} ";// redirect ? @"/c " + "\"" + url +"\"" : @"/k " + "\"" + url + "\"";
proc.Start();
proc.StandardInput.WriteLine(cmd);// (@"net use \\172.25.138.150User@123 /user:administrator");//xcopy \\eahis\netlogon\bmp c:\bmp /e/y
proc.StandardInput.WriteLine("exit");
while (!proc.HasExited)
{
proc.WaitForExit(1000);
}
string errormsg = proc.StandardError.ReadToEnd();
proc.StandardError.Close();
if (string.IsNullOrEmpty(errormsg))
{
// Flag = true;
}
else
{
throw new Exception(errormsg);
}
proc.Close();
// proc.Dispose();
return "";
}
//共享用户连接
public static bool connect(string dosLine)
{
try
{
return connectState(dosLine);
}
catch (Exception ex)
{
if (cleanConnect())
{
return connectState(dosLine);
}
}
return false;
}
public static bool connectState(string dosLine)
{
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 " + path + " /User:" + userName + " " + passWord + " /PERSISTENT:YES";
proc.StandardInput.WriteLine(dosLine);
proc.StandardInput.WriteLine("exit");
while (!proc.HasExited)
{
proc.WaitForExit(1000);
}
string errormsg = proc.StandardError.ReadToEnd();
proc.StandardError.Close();
if (string.IsNullOrEmpty(errormsg))
{
Flag = true;
}
else
{
throw new Exception(errormsg);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
proc.Close();
proc.Dispose();
}
return Flag;
}
public static bool cleanConnect()
{
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 " + path + " /User:" + userName + " " + passWord + " /PERSISTENT:YES";
proc.StandardInput.WriteLine(" net use * /del /y");
proc.StandardInput.WriteLine("exit");
while (!proc.HasExited)
{
proc.WaitForExit(1000);
}
string errormsg = proc.StandardError.ReadToEnd();
proc.StandardError.Close();
if (string.IsNullOrEmpty(errormsg))
{
Flag = true;
}
else
{
// throw new Exception(errormsg);
}
}
catch (Exception ex)
{
// throw ex;
}
finally
{
proc.Close();
proc.Dispose();
}
return Flag;
}
//手动复制拷贝
public static void CopyDir(string srcPath, string aimPath)
{
try
{
// 检查目标目录是否以目录分割字符结束如果不是则添加之
if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
aimPath += Path.DirectorySeparatorChar;
// 判断目标目录是否存在如果不存在则新建之
if (!Directory.Exists(aimPath))
Directory.CreateDirectory(aimPath);
// 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
//如果你指向copy目标文件下面的文件而不包含目录请使用下面的方法
//string[] fileList = Directory.GetFiles(srcPath);
string[] fileList = Directory.GetFileSystemEntries(srcPath);
//遍历所有的文件和目录
foreach (string file in fileList)
{
//先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
if (Directory.Exists(file))
CopyDir(file, aimPath + Path.GetFileName(file));
//否则直接Copy文件
else
File.Copy(file, aimPath + Path.GetFileName(file), true);
}
}
catch (Exception ee)
{
throw new Exception(ee.ToString());
}
}
}
//调用demo
string res = ToolMoveFiles.MoveDirectory(@"\\10.122.55.3\model\abc", @"E:\abc\");
期间走不不少弯路,也查过许多资料。一开始没有设置共享用户连接时,本地运行正常,可是发布到服务器会拷贝失败。再此做个记录,也希望能帮上其他人。
本文:如何复制一个列表
最简单的方法是 foreach
foreach(var temp in a)
{
b.Add(temp);
}
有没一个简单的方法?
using System.Linq;
var a = new List<Fex>()
{
new Fex() {F = true,},
new Fex() {F = true,},
new Fex() {F = false,},
};
List<Fex> b = a.ToList();
b.RemoveAt(0);
Console.WriteLine(a.Count);
b.Add(new Fex());
b.Add(new Fex());
Console.WriteLine(a.Count);
List<Fex> b = a.ToList();
可以把列表a到列表b,对b进行删除、添加,不会对a造成元素改变。
本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。欢迎转载、使用、重新发布,但务必保留文章署名林德熙(包含链接:http://blog.csdn.net/lindexi_gd ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系。