复制目录不起作用

时间:2014-09-26 02:50:50

标签: vb.net vb.net-2010

我正在尝试使用以下代码将目录复制到另一个目录中,但出于某种原因,它只复制目录的内容而不是目录本身。

        Try
        My.Computer.FileSystem.CopyDirectory(TextBox1.Text, My.Settings.FSXLocation & "\SimObjects\Airplanes", True)
        MsgBox("Your 737-800 Aircraft has now been Installed.", MsgBoxStyle.Information, "Figerty Systems Inc")
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical, "Figerty Systems Inc.")
    End Try

有没有人有任何想法?

修改 - 更多代码:

        Try
        Dim arg = String.Format("{0} {1} /i/y/s/e/D", Path.GetFullPath(TextBox1.Text), Path.GetFullPath(My.Settings.FSXLocation & "\SimObjects\Airplanes"))
        Dim p = New Process()
p.StartInfo = New ProcessStartInfo() With { _
    Key .FileName = "xcopy", _
    Key .Arguments = arg, _
    Key .UseShellExecute = False, _
    Key .RedirectStandardOutput = True _
}
        p.Start()
        Console.WriteLine(p.StandardOutput.ReadToEnd())
        p.WaitForExit()
    Catch ex As Exception
        Console.WriteLine("[FAIL] COPY: {0}", ex.Message)
        Throw
    End Try

3 个答案:

答案 0 :(得分:0)

Windows API不会递归地支持复制目录。 我只是告诉你csharp代码:

        try {
            var arg = string.Format("{0} {1} /i/y/s/e/D", Path.GetFullPath("../www"), Path.GetFullPath("./EM/www"));
            var p = new Process();
            p.StartInfo = new ProcessStartInfo {
                FileName = @"xcopy",
                Arguments = arg,
                UseShellExecute = false,
                RedirectStandardOutput = true
            };
            p.Start();
            Console.WriteLine(p.StandardOutput.ReadToEnd());
            p.WaitForExit();
        } catch (Exception ex) {
            Console.WriteLine("[FAIL] COPY: {0}", ex.Message);
            throw;
        }

愿它有所帮助。

答案 1 :(得分:0)

看看this。它已经为您实施了。但如果你真的想重新发明轮子,如果我是你,我会考虑递归。

答案 2 :(得分:0)

这是我第一次写vb.net代码,

  

Imports System.IO

Module Module1

    Sub Main()
    Try
        Dim arg = String.Format("{0} {1} /i/y/s/e/D", Path.GetFullPath("src"), Path.GetFullPath("dest"))
        Dim p = New Process()
        p.StartInfo = New ProcessStartInfo() With {
            .FileName = "xcopy",
            .Arguments = arg,
            .UseShellExecute = False,
            .RedirectStandardOutput = True
        }

        p.Start()
        Console.WriteLine(p.StandardOutput.ReadToEnd())
        p.WaitForExit()
    Catch ex As Exception
        Console.WriteLine("[FAIL] COPY: {0}", ex.Message)
        Throw
    End Try
    End Sub

End Module