English 中文(简体)
• 非统计环境中的目录?
原标题:Copying Directories in a non-static environment?

Can some one give me a hint/example of copying a directory in a non-static environment? I tried to look it up but no such luck.

    private void copybutton_Click(object sender, EventArgs e)
    {
        string selectedPath = comboBox1.SelectedItem.ToString();
        copydirectories(@"C:MavroMavBridge" + selectedPath, @"C:MavroMavBridge" + selectedPath + " - Copy");
    }

    public void copydirectories()
    {
        try
        {
            string selectedPath = comboBox1.SelectedItem.ToString();
            DirectoryInfo source = new DirectoryInfo(@"C:MavroMavBridge" + selectedPath);
            DirectoryInfo target = new DirectoryInfo(@"C:MavroMavBridge" + selectedPath);

            if (!source.Exists)
                return;
            if (!target.Exists)
                target.Create();

            DirectoryInfo[] sourceDirectories = source.GetDirectories();    
            for(int j = 0; j < sourceDirectories.Length; ++j)
            {
                copydirectories(sourceDirectories[j].FullName,target.FullName +"\" + sourceDirectories[j].Name);
            }
            datemodified.Refresh();

   }

这只能得出2个论点错误。

最佳回答

http://xneuron.wordpress.com/2007/04/12/copy-directory-and-its-content-to-another-directory-in-c/"rel=“nofollow” http://xneuron.wordpress.com/2007/04/12/copy-directory-and-its-content-to-another-directory-in-c/

public static void CopyAll(DirectoryInfo source, DirectoryInfo target) {
    // Check if the target directory exists, if not, create it.
    if (!Directory.Exists(target.FullName)) {
        Directory.CreateDirectory(target.FullName);
    }

    // Copy each file into its new directory.
    foreach (FileInfo fi in source.GetFiles()) {
        Console.WriteLine(@”Copying {0}{1}”, target.FullName, fi.Name);
        fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
    }

    // Copy each subdirectory using recursion.
    foreach (DirectoryInfo diSourceSubDir in source.GetDirectories()) {
        DirectoryInfo nextTargetSubDir = 
            target.CreateSubdirectory(diSourceSubDir.Name);
        CopyAll(diSourceSubDir, nextTargetSubDir);
    }
}

makes it easy, just call it with

CopyAll(new DirectoryInfo(@"C:MavroMavBridge" + selectedPath),
        new DirectoryInfo(@"C:MavroMavBridge" + selectedPath + " - Copy"));
问题回答

你们是否想以2个论点来建立职能?

public void copydirectories(string sourcePath, string destinationPath)
    {
        try
        {
            DirectoryInfo source = new DirectoryInfo(sourcePath);
            DirectoryInfo target = new DirectoryInfo(destinationPath);




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签