Ошибка в ходе выполнения Directory.Move - C#

Узнай цену своей работы

Формулировка задачи:

Нужно скопировать некоторые данные. Написал прогу, но на Directory.Move выскакивает ошибка в ходе выполнения.
An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll

Additional information: Source and destination path must have identical roots. Move will not work across volumes.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
 
namespace getDrive
{
    class Program
    {
        static void Main(string[] args)
        {
            DriveInfo[] allDrives = DriveInfo.GetDrives();
            foreach (DriveInfo d in allDrives)
            {
                if(d.IsReady)
                    if ((d.TotalSize > 1000000) && (d.DriveType == DriveType.Removable))
                    {
                        string[] strForCatalogs = Directory.GetDirectories(d.Name);
                        string strDirectory = @"C:\" + d.VolumeLabel;
                        string[] strFiles = Directory.GetFiles(d.Name);
                        Directory.CreateDirectory(strDirectory);
                        foreach (string s_1 in strForCatalogs)
                            Directory.Move(s_1, strDirectory);
                        foreach (string s_1 in strFiles)
                            Directory.Move(s_1, strDirectory);
                    }
            }
            Console.ReadKey();
        }
    }
}
Что с ошибкой делать? P.S. очень интересует вопрос. На с++ можно было создать пустой проект так, чтобы вся прога работала без интерфейса. Достаточно было создать "пустой проект" и всё писать на win api. Как можно создать прогу без интерфейса на C#?
ах да.. совсем забыл. В MSDN не нашёл метода Copy, только Move

Решение задачи: «Ошибка в ходе выполнения Directory.Move»

textual
Листинг программы
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
 
namespace WindowsFormsApplication15
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main () {
            #region Creating a window
            var wndp = new Win32.WNDCLASS();
            var hWnd = IntPtr.Zero;
            var msg  = new Win32.MSG();
 
            wndp.lpfnWndProc   = WndProc;
            wndp.lpszClassName = "MsgOnlyWnd";
            wndp.hInstance     = Win32.GetModuleHandle(null);
            // Register window class
            if (Win32.RegisterClass(ref wndp) == 0)
                throw new Win32Exception(Marshal.GetLastWin32Error());
            // Create invisible window
            hWnd = Win32.CreateWindowEx(
                0U, wndp.lpszClassName, null,
                0U, 0, 0, 0, 0, IntPtr.Zero, IntPtr.Zero,
                wndp.hInstance, IntPtr.Zero);
 
            if (hWnd == IntPtr.Zero)
                throw new Win32Exception(Marshal.GetLastWin32Error());
            #endregion
 
            // Your code here
 
            #region Message loop
            // Message loop
            while(Win32.GetMessage(out msg, hWnd, 0U, 0U)) {
                Win32.TranslateMessage(ref msg);
                Win32.DispatchMessage(ref msg);
            }
            // Unregister window class
            Win32.UnregisterClass(wndp.lpszClassName, wndp.hInstance);
            #endregion
        }
 
        static IntPtr WndProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam) {
            switch(uMsg) {
                case 0x002U: // WM_DESTROY
                case 0x012U: // WM_QUIT
                    Win32.PostQuitMessage(0);
                    return IntPtr.Zero;
                case 0x219U: // WM_DEVICECHANGE
                    // TODO:
                    return IntPtr.Zero;
            }
 
            return Win32.DefWindowProc(hWnd, uMsg, wParam, lParam);
        }
    }
 
    static class Win32 {
        const string USER32   = "user32.dll";
        const string KERNEL32 = "kernel32.dll";
        public delegate IntPtr WNDPROC(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam);
 
        [DllImport(USER32, SetLastError = true)]
        public static extern IntPtr CreateWindowEx(
            uint   dwExStyle,
            string lpClassName,
            string lpWindowName,
            uint   dwStyle,
            int    x,
            int    y,
            int    nWidth,
            int    nHeight,
            IntPtr hWndParent,
            IntPtr hMenu,
            IntPtr hInstance,
            IntPtr lpParam
            );
 
        [DllImport(KERNEL32, SetLastError = true)]
        public static extern IntPtr GetModuleHandle(
            string lpModuleName
            );
 
        [DllImport(USER32, SetLastError = true)]
        public static extern IntPtr DefWindowProc(
            IntPtr hWnd,
            uint   uMsg,
            IntPtr wParam,
            IntPtr lParam
            );
 
        [DllImport(USER32, SetLastError = true)]
        public static extern ushort RegisterClass(
            ref WNDCLASS lpWndClass
            );
 
        [DllImport(USER32, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool UnregisterClass(
            string lpClassName,
            IntPtr hInstance
            );
 
        [DllImport(USER32, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetMessage(
            out MSG lpMsg,
            IntPtr hWnd,
            uint wMsgFilterMin,
            uint wMsgFilterMax
            );
 
        [DllImport(USER32, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool TranslateMessage(
            ref MSG lpMsg
            );
 
        [DllImport(USER32, SetLastError = true)]
        public static extern IntPtr DispatchMessage(
            ref MSG lpMsg
            );
 
        [DllImport(USER32, SetLastError = true)]
        public static extern void PostQuitMessage(
            int nExitCode
            );
 
        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        public struct WNDCLASS {
            public uint    style;
            public WNDPROC lpfnWndProc;
            public int     cbClsExtra;
            public int     cbWndExtra;
            public IntPtr  hInstance;
            public IntPtr  hIcon;
            public IntPtr  hCursor;
            public IntPtr  hbrBackground;
            public string  lpszMenuName;
            public string lpszClassName;
        }
 
        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        public struct MSG {
            public IntPtr hwnd;
            public uint   message;
            public IntPtr wParam;
            public IntPtr lParam;
            public uint   time;
            public POINT  pt;
        }
 
        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        public struct POINT {
            public int X;
            public int Y;
        }
    }
}

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

7   голосов , оценка 4.143 из 5
Похожие ответы