移动目录 [英] Moving Directory

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

问题描述

你好,
我在将目录移动到另一个位置时遇到问题.每当我尝试移动它时,它会告诉我该文件已存在.这是代码

Hello,
I''m having problems with moving a directory to another location. Everytime i try to move it, it tells me the file already exists. Here is the code

Directory.Move(textBox3.Text, textBox2.Text);


textBox2是我要将目录移动到的文件夹的路径. textBox3是我要移动的目录的路径.我得到的只是一个IOException当该文件已经存在时无法创建文件.香港专业教育学院搜索这个问题已经有好几个小时了,没有运气.谢谢

克里斯汀·格劳斯(Christian Graus):是的,我知道命名约定使其易于阅读.是的,我的文本框源和目标是正确的.即使它不正确,也只会将另一个文件夹移到我要移动的文件夹中,但是它不起作用.

DaveyM69:是的,我多次阅读该链接.我尝试将源文件夹追加到目标路径,但是我似乎在删除文件夹的整个路径而不是仅使用文件夹名称时遇到问题.因此,当我附加它时,它给我一个错误不支持给定路径的格式".您知道我如何删除整个路径,所以我只是可以附加并尝试使用的Pakoo名称?

还要感谢你们俩为我提供的帮助.


DaveyM69:谢谢.完美的作品.再次感谢


textBox2 is the path for the folder i want the directory to be moved to. textBox3 is the path for the directory i want to move. All i keep getting is an IOException Cannot create a file when that file already exists. Ive searched for this problem for hours now with no luck. Thanks

Christian Graus: Yes i am aware naming conventions make it readable. Yes my textbox source and dest are correct. Even if it was not correct, it would just move the other folder into the one i want to move, however it is not working.

DaveyM69: Yes i read that link several times lol. I''ve tried appending the source folder to the destination path, however i seem to have problems with removing the folders entire path instead of it just using the folder name. So when i append it, it gives me an error "The given path''s format is not supported". Do you know how i could remove the whole path, so i just have the Pakoo name that i can append and attempt?

And thanks to both of you for helping me with this.


DaveyM69: Thank you. Works perfectly. Thanks again

推荐答案

我怀疑您没有将最终目录名称附加到目标路径中.

查看 [
I suspect you''re not appending the final directory name to your destination path.

Look at the remarks section on this[^] page.

Using your example, textBox2.Text would need to be
C:\Users\ME\Downloads\Pakoo
not
C:\Users\ME\Downloads
as C:\Users\ME\Downloads already exists.


我刚刚测试了这段代码,它似乎可以满足您的要求,只需更改Textbox es和添加错误检查/异常捕获.
I''ve just tested this code and it seems to do what you want, just change the hardcoded values for the ones from your Textboxes and add error checking/exception catching.
//Moves the 'Test' folder
string source = @"C:\Users\Dave\Documents\Test";
string destination = Path.Combine(
    @"C:\Users\Dave", new DirectoryInfo(
        source).Name);
Directory.Move(source, destination);


好吧,您确定参数顺序正确吗?绝对确定目录不存在吗?
Well, are you certain you have the parameters in the right order ? Absolutely SURE the directory does not exist ?


这篇关于移动目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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