代码之家  ›  专栏  ›  技术社区  ›  mhenrixon

如何复制DirectoryNotFoundException?

c#
  •  1
  • mhenrixon  · 技术社区  · 15 年前

    我有一个使用幻影做递归删除和复制的问题。我已经解决了实际的问题,这个问题很容易检查下面的异常,任何人都可以轻松地进行检查,但是您能告诉我如何从Visual Studio中重现这个问题吗?

    System.IO.DirectoryNotFoundException(系统.IO.DirectoryNotFound异常): 找不到路径的一部分 “build/webui\views/web.config”。在 System.IO.\u错误.WinIOError(Int32 错误代码,字符串maybefullpath)at system.io.file.internalcopy(字符串 sourcefilename,string目标文件名, 布尔值覆盖)at 幻影.core.wrappedfileinfo.copytoddirectory(字符串 路径) d:\opensource\build\phantom2\phantom\src\phantom.core\wrappedfileinfo.cs:line 75 AT 生成。$$执行$closure$23$closure$25.invoke(wrappedfilesysteminfo 文件) 幻影.core.builtins.utilityFunctions.foreach[t](IEnumerable 1 source, Action 1行动) d:\opensource\build\phantom2\phantom\src\phantom.core\builtins\utilityfunctions.cs:line 34 AT build.$execute$closure$23.invoke())
    在中的phantom.core.target.execute()处 d:\opensource\build\phantom2\phantom\src\phantom.core\target.cs:line 81 AT 幻影.core.scriptmodel.executeTargets(字符串[] 目标名称 d:\opensource\build\phantom2\phantom\src\phantom.core\scriptmodel.cs:line 73 AT 幻影.program.execute(string[]参数) 在里面 d:\opensource\build\phantom2\phantom\src\phantom\program.cs:line 五十七

    问题是,幻影是从.bat/.cmd文件启动的,生成文件作为参数发送,但在我的Windows 7 x64计算机上失败了,但在Visual Studio 2008中执行完全相同的操作,因此即使我复制了VIEWS文件夹,也可以递归执行。原始代码(从命令行失败):

    public override void CopyToDirectory(string path) {
        if (!Directory.Exists(path)) {
            Directory.CreateDirectory(path);
        }
    
        if (Flatten) {
            var combinedPath = Path.Combine(path, Name);
            File.Copy(FullName, combinedPath, true);
        }
        else {
            var combinedPath = Path.Combine(path, PathWithoutBaseDirectory);
            File.Copy(FullName, combinedPath, true);
        }
    }
    

    将上述内容更改为:

    public override void CopyToDirectory(string path) {
        if (!Directory.Exists(path)) {
            Directory.CreateDirectory(path);
        }
    
        if (Flatten) {
            var combinedPath = Path.Combine(path, Name);
            File.Copy(FullName, combinedPath, true);
        }
        else {
            var combinedPath = Path.Combine(path, PathWithoutBaseDirectory);
            var newPath = Path.GetDirectoryName(combinedPath);
            if (!Directory.Exists(newPath)) {
                Directory.CreateDirectory(newPath);
            }
            File.Copy(FullName, combinedPath, true);
        }
    }
    

    使那个特定的问题消失,这很好。我的意思是只花了几分钟就弄清楚了,但是试图重现这个问题或创建一个失败的测试现在已经花了好几个小时,我想知道为什么不能完成或学习如何做:)

    编辑: 这样做的原因是非常愚蠢的,但现在有了。在这种情况下,基目录将是“views”,带有ourbasedirectory的路径将等于“home/about.aspx”,darn在cmd中不起作用,但它在Visual Studio中起作用。接下来我知道我也不能让它在Visual Studio中运行,所以我应用了修复程序,所有测试都变为绿色。我还是不知道为什么…

    1 回复  |  直到 15 年前
        1
  •  0
  •   Matthias    15 年前

    我想知道为什么

    嗯-有几个问题:

    假设路径是“c:\foo\bar.txt”(其中bar.txt是文本文件)。
    现在您正在调用directory.createddirectory(path)。

    您尝试创建目录“c:\foo\bar.txt”。然而,这已经是一个文件了。

    然后,在创建文件时,将此路径与文件名结合起来-结果可能是“c:\foo\bar.txt\bar.txt”。然而,这是不可能成功的。