将文件夹内容移动到另一个文件夹 [英] Move contents of folder to another folder

查看:303
本文介绍了将文件夹内容移动到另一个文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有一个与我的应用程序位于同一目录中的文件夹,我需要将此文件夹的内容移动到用户已选择的另一个文件夹,并且该目标文件夹的路径显示在文本框中。





需要什么



1)申请获得当前目录位置

2)访问该位置的文件夹(此文件夹名为folderA)

3)然后复制folderA中的文件

4)到已设置的目录但用户及其路径显示在文本框中(称为opendir)



我假设你会从opendir.text拉出目标文件夹路径





  string  files = System.IO.Directory.GetCurrentDirectory(); 

try
{
Process.Start(Openfile.Text);
WindowState = WindowState.Minimized;
Directory.Move(files + @ \ FolderA,Opendir.Text);
}
catch
{
// < span class =code-comment> MessageBox.Show(,Error!,MessageBoxButton.OK,MessageBoxImage.Error);
}





这是我到目前为止所得到的但我似乎无法在GetCurrentDirectory调用后将程序指向folderA。

解决方案

< blockquote> Directory.Move会在很多条件下完成它。请参阅MSDN,了解它何时不会: https: //msdn.microsoft.com/en-us/library/system.io.directory.move(v=vs.110).aspx [ ^ ]



或许,您需要做的是更改Source参数。试试这个:

 Directory.Move(Path.Combine(Application.StartupPath,  FolderA),Opendir.Text); 






除了很明显你没有使用谷歌搜索找到你的解决方案,但你刚刚问到这里希望现在或以后的某个人会给你一个解决方案,这就是你需要做的事情。



首先,我建议您从MSDN阅读本文档: https://msdn.microsoft.com/en-us/library/vstudio/ms404278(v = vs.100).aspx [ ^ ]。 MSDN是一个巨大的网站,几乎可以找到它的所有内容。我没有在Codeproject上寻找解决方案,但我认为有一个,必须有一个。



其次,Directory.Move正是你想要的,所以你可能想要使用递归函数或foreach语句,甚至 Microsoft.VisualBasic命名空间。以下是 http://stackoverflow.com的参考资料/ questions / 3911595 / move-all-files-in-subfolders-to-another-folder-using-c-sharp [ ^ ]。



我希望这会有所帮助,并为下一次提出一些建议:谷歌提出你的问题:它可能已被问过一百万次。而且,你会早点得到答案! :-)



LG


我通过以下代码找到了解决方案,感谢OriginalGriff

 Directory.Move(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory,FolderA),Opendir.Text); 


Hi i have a folder which is in the same directory as my application and i need to move the contents of this folder to another folder which has been selected by the user and the path of this destination folder is displayed in a textbox.


What is needed

1)The application to get its current directory location
2)Access a folder that is in that location (This folder is named folderA)
3)Then to copy the files that are in folderA
4)To the directory which has been set but the user and its path is displayed in a textbox (called opendir)

I am assuming you would pull the destination folder path from opendir.text


string files = System.IO.Directory.GetCurrentDirectory();
            
            try
            {
                Process.Start(Openfile.Text);
                WindowState = WindowState.Minimized;
                Directory.Move(files + @"\FolderA", Opendir.Text);
            }
            catch
            {
                //MessageBox.Show("", "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
            }



This is what i have got so far but i cant seem to direct the program to folderA after the GetCurrentDirectory call.

解决方案

Directory.Move will do it, under many conditions. See MSDN for when it won't: https://msdn.microsoft.com/en-us/library/system.io.directory.move(v=vs.110).aspx[^]

Probably, what you need to do is change the Source parameter. Try this:

Directory.Move(Path.Combine(Application.StartupPath, "FolderA"), Opendir.Text);


Hi,

Apart from the fact that it is clear that you have not googled to find your solution, but you've just asked here hoping that somebody, now or later, will give you a solution here's what you need to do.

First of all I suggest you to read this document from MSDN: https://msdn.microsoft.com/en-us/library/vstudio/ms404278(v=vs.100).aspx[^]. MSDN is a huge website you can find almost everything on it. I haven't looked for a solution on Codeproject, but I assume there is one, there MUST be one.

Secondly, Directory.Move does exactly what you want, so you may want to use a recursive function or a foreach statement or even Microsoft.VisualBasic namespace. Here's a reference http://stackoverflow.com/questions/3911595/move-all-files-in-subfolders-to-another-folder-using-c-sharp[^].

I hope this helps and, a piece of advice for the next times: google for your question: it might have been asked one million times before. Moreover, you will get an answer sooner! :-)

LG


I have found the solution with the following code thanks to OriginalGriff

Directory.Move(System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "FolderA"), Opendir.Text);


这篇关于将文件夹内容移动到另一个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆