[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)] [MarshalAs(UnmanagedType.U4)] public int wFunc; [MarshalAs(UnmanagedType.Bool)] public bool fAnyOperationsAborted; [DllImport("shell32.dll", CharSet=CharSet.Auto)] 出错 怎么回事?
//determine if the destination directory exists, if not create it if (! Directory.Exists(destdir)) { Directory.CreateDirectory(destdir); }
dir = new DirectoryInfo(srcdir);
//if the source dir doesn't exist, throw if (! dir.Exists) { throw new ArgumentException("source dir doesn't exist -> " + srcdir); }
//get all files in the current dir files = dir.GetFiles();
//loop through each file foreach(FileInfo file in files) { //create the path to where this file should be in destdir tmppath=Path.Combine(destdir, file.Name);
//copy file to dest dir file.CopyTo(tmppath, false); }
//cleanup files = null;
//if not recursive, all work is done if (! recursive) { return; }
//otherwise, get dirs dirs = dir.GetDirectories();
//loop through each sub directory in the current dir foreach(DirectoryInfo subdir in dirs) { //create the path to the directory in destdir tmppath = Path.Combine(destdir, subdir.Name);
//recursively call this function over and over again //with each new dir. FileCopy(subdir.FullName, tmppath, recursive); }
static void XCopy( string FromFolderPath , string ToFolderPath )
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
string strCommand = "xcopy " + FromFolderPath + " " + ToFolderPath;
p.StandardInput.WriteLine(strCommand);
p.StandardInput.WriteLine("D");
p.Close();
}
我传进去两个文件夹路径
怎么不可以吗?
XCopy( "e:\\abc" , "d:\\test" )
abc为要拷贝的目标文件夹,test为目的地文件夹
代码没出错跑过去
了
但没有效果,什么也没有拷过去
好相是这样。
{
if(!Directory.Exists(dirFrom)) throw(new Exception(dirFrom+" not exists"));
if(Directory.Exists(dirTo)) throw(new Exception(dirTo+" exists"));
Directory.CreateDirectory(dirTo);
try{
foreach(string f in Directory.GetFiles(dirFrom)){
File.Copy(f,dirTo+"\\"+Path.GetFileName(f));
}
foreach(string d in Directory.GetDirectories(dirFrom)){
dirCopy(d,dirTo+"\\"+Path.GetFileName(d));
}
return 0;
}
catch(Exception){ return -1;}
}
xcopy命令是可以复制文件夹的
有已用过的c#代码吗?哪上面的一堆
我拷贝下来一堆错误!
[MarshalAs(UnmanagedType.U4)] public int wFunc;
[MarshalAs(UnmanagedType.Bool)] public bool fAnyOperationsAborted;
[DllImport("shell32.dll", CharSet=CharSet.Auto)] 出错
怎么回事?
using System;
using System.IO;
namespace FrameworkExamples
{
//HOW TO: recursively copy all the files and sub dirs in a given directory
// to another directory
class SampleRecursiveCopy
{
static void Main()
{
string srcdir, destdir;
bool recursive;
recursive = true;
srcdir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "images");
destdir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "images2");
FileCopy(srcdir, destdir, recursive);
}
private static void FileCopy(string srcdir, string destdir, bool recursive)
{
DirectoryInfo dir;
FileInfo[] files;
DirectoryInfo[] dirs;
string tmppath;
//determine if the destination directory exists, if not create it
if (! Directory.Exists(destdir))
{
Directory.CreateDirectory(destdir);
}
dir = new DirectoryInfo(srcdir);
//if the source dir doesn't exist, throw
if (! dir.Exists)
{
throw new ArgumentException("source dir doesn't exist -> " + srcdir);
}
//get all files in the current dir
files = dir.GetFiles();
//loop through each file
foreach(FileInfo file in files)
{
//create the path to where this file should be in destdir
tmppath=Path.Combine(destdir, file.Name);
//copy file to dest dir
file.CopyTo(tmppath, false);
}
//cleanup
files = null;
//if not recursive, all work is done
if (! recursive)
{
return;
}
//otherwise, get dirs
dirs = dir.GetDirectories();
//loop through each sub directory in the current dir
foreach(DirectoryInfo subdir in dirs)
{
//create the path to the directory in destdir
tmppath = Path.Combine(destdir, subdir.Name);
//recursively call this function over and over again
//with each new dir.
FileCopy(subdir.FullName, tmppath, recursive);
}
//cleanup
dirs = null;
dir = null;
}
}
}
public class ShellFiles
{
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto, Pack=1)]
public struct SHFILEOPSTRUCT
{
public IntPtr hwnd;
[MarshalAs(UnmanagedType.U4)] public int wFunc;
public string pFrom;
public string pTo;
public short fFlags;
[MarshalAs(UnmanagedType.Bool)] public bool fAnyOperationsAborted;
public IntPtr hNameMappings;
public string lpszProgressTitle;
}
[DllImport("shell32.dll", CharSet=CharSet.Auto)]
static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);
const int FO_DELETE = 3;
const int FO_COPY = 2;
const int FOF_ALLOWUNDO = 0x40;
const int FOF_NOCONFIRMATION = 0x10; //Don't prompt the user.;
const int FOF_SIMPLEPROGRESS = 0x100;
public void SendToRecyclyBin(string path)
{
SHFILEOPSTRUCT shf = new SHFILEOPSTRUCT();
shf.wFunc = FO_DELETE;
shf.fFlags = FOF_ALLOWUNDO ¦ FOF_NOCONFIRMATION;
shf.pFrom = path;
SHFileOperation(ref shf);
}
public void Copy(string from, string to)
{
DirectoryInfo source = new DirectoryInfo(from);
DirectoryInfo dest = new DirectoryInfo(to);
if(!dest.Exists)
dest.Create();
SHFILEOPSTRUCT shf = new SHFILEOPSTRUCT();
shf.wFunc = FO_COPY;
shf.fFlags = FOF_ALLOWUNDO;
shf.pFrom = from;
shf.pTo = to;
SHFileOperation(ref shf);
}
}
public sealed class DirectoryUtils
{
private DirectoryUtils()
{
}
/// <summary>
/// Copies a directory to a new location.
/// </summary>
/// <param name="src">Source directory path</param>
/// <param name="dest">Destination directory path</param>
public static void CopyDirectory(String src, String dest)
{
DirectoryInfo di = new DirectoryInfo(src);
foreach(FileSystemInfo fsi in di.GetFileSystemInfos())
{
String destName = Path.Combine(dest, fsi.Name);
if (fsi is FileInfo)
File.Copy(fsi.FullName, destName);
else
{
Directory.CreateDirectory(destName);
CopyDirectory(fsi.FullName, destName);
}
}
}
}
用那个API即可。