<strong><span style="font-size:18px;">public class File
{
/// <summary>
/// copy文件夹下的所有文件
/// </summary>
/// <param name="oPath">要拷贝的文件夹路径</param>
/// <param name="nPath">目标路径</param>
public static void MoveFile(string oPath, string nPath)
{
string flag = "下载成功";
try
{
if(!Directory.Exists(nPath))
{
Directory.CreateDirectory(nPath);
}
DirectoryInfo sDir = new DirectoryInfo(oPath);
FileInfo[] fileArray = sDir.GetFiles();
foreach(FileInfo file in fileArray)
{
file.CopyTo(nPath + "\\" + file.Name , true);
}
DirectoryInfo dDir = new DirectoryInfo(nPath);
DirectoryInfo[] subDirArray = sDir.GetDirectories();
foreach(DirectoryInfo subDir in subDirArray)
{
MoveFile(subDir.FullName , nPath + "\\" + subDir.Name);
}
isSuccess = true;
}
catch(Exception ex)
{
flag = ex.ToString();
}
}
/// <summary>
/// 是否成功下载
/// </summary>
public static bool isSuccess = false;
}</span></strong>
<strong><span style="font-size:18px;">配合调出路径选择的对话框:</span></strong>
<pre class="csharp" name="code"><strong><span style="font-size:18px;"> FolderBrowserDialog fbd = new FolderBrowserDialog();//选择文件的对话框是openfiledialog
if (fbd.ShowDialog() == DialogResult.OK)
{
App app = listView1.SelectedItems[0].Tag as App;
int idx = app.AppAddress.LastIndexOf('\\');
if (idx >= 0)
{
string path = fbd.SelectedPath + "\\" + app.AppAddress.Substring(idx + 1);
</span></strong>
<strong><span style="font-size:18px;"> //对话框选择路径的获取 fbd.SelectedPath</span></strong>
<strong><span style="font-size:18px;"> File.MoveFile(app.AppAddress, path);</span></strong>
<strong><span style="font-size:18px;">//为了保证选择的路径下也有原文件夹,所以要在对话框内选择的路径后面加上原文 件夹的名字。这部分操作是:根据原来文件夹路径的最后一个"\",截取后面的字符串,再加到选择的目标路径后面。</span></strong>
<strong><span style="font-size:18px;"> string a = "wdadada\\awdadw\\aweadawd\\awdawdaw\\aasdad";</span></strong>
<strong><span style="font-size:18px;"> int idx = a.LastIndexOf('\\');</span></strong>
<strong><span style="font-size:18px;"> if (idx >= 0) string path = a.SubString(idx + 1);</span></strong>
目标路径加上path