给定路径不支持在file.copy方法中发生异常

时间:2016-03-22 13:26:48

标签: c#

我尝试将主列表xml文件复制到bin \ Debug文件夹。但它抛出异常。我在控制台应用程序中传递了参数,如"C:\Users\gio.frog\Desktop\mainlist.xml",并使用了file.copy方法,如下所示:

File.Copy(args[0], AppDomain.CurrentDomain.BaseDirectory + "sublist.xml",true);

我在stackoverflow中看到了一些相同的异常示例,但无法得到正确的理由。如何复制到bin / debug文件夹。?

1 个答案:

答案 0 :(得分:2)

AppDomain.CurrentDomain.BaseDirectory在结尾处返回没有\的路径。当你将它与尝试结合时,它将与目录名连接。

E.g。 "c:\projects\Debug\bin" + "sublist.xml"会产生"c:\projects\Debug\binsublist.xml"

使用

Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "sublist.xml");