0

想通了问题 1。请参阅问题 2。仍然需要帮助。请参阅我对问题 1 的回答

这是一个基于控制台 C# 的程序

问题 1

我想循环创建目录创建。我正在使用以下方法复制目录(请暂时忽略使用 Do While ...我只希望设备编号正确且位于正确的位置)。现在假设我们每次手动运行应用程序。我希望在每次执行时列表的命名和编号如下所示,它应该构建一个列表,如下所示...

C:\\某些位置\\设备

地点内...

  • 设备 1 文件夹,里面有内容
  • 设备 2 文件夹,里面有内容
  • 设备 3 文件夹,里面有内容

等等......换句话说,我需要某种循环来用下一个索引替换设备的命名

这是正在使用的方法...

  • 路径 - 通过用户读入。例如 "D:\\" 复制 D:\\ 驱动器内容
  • True - 复制子目录的默认值,而不仅仅是预期的目录

    DirectoryCopy(path, @"C:\\Custom Location to duplicate to", true);
    

方法实现:

private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
    {
        if (Directory.Exists(sourceDirName))
        {
            cancontinue = true;

            DirectoryInfo dir = new DirectoryInfo(sourceDirName);

            //Make an Array of Directories found on The Device
            DirectoryInfo[] dirs = dir.GetDirectories();

            if (!Directory.Exists(destDirName))
            {
                Directory.CreateDirectory(destDirName);
            }
            else
            {

                //Provide a Device Listing. Here is Where I am Stuck

                for (int i = 1; i < 11; i++)
                {
                    if (Directory.Exists(destDirName)) 
                    {
                        destDirName = destDirName + "\\Device\\ " + i.ToString();
                    }
                    else
                    {
                        break;
                    }
                }
                Directory.CreateDirectory(destDirName);
            }

接下来是以相同方式复制子目录的循环。当前解决方案提供以下输出...

C:\自定义位置\设备\设备 1\内容

并且在设备 1 中,第二个设备将复制,然后在其中复制下一个,依此类推。

问题2

我想将它复制到的路径自定义为机器名称(用于发布目的)

    //This Gets the Name Perfectly + The Added Desired Path Below
    string MachineName = System.Environment.MachineName;
    string DesiredPath = "\\Desktop\\Program\\"; 

    DirectoryCopy(path, @"C:\Users\" + MachineName + DesiredPath, true);

问题这是我收到拒绝访问错误???这是为什么?有解决办法吗?

4

2 回答 2

1

问题一:

for 循环不应覆盖 destDirName 变量:

private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
    if (Directory.Exists(sourceDirName))
    {
        cancontinue = true;

        DirectoryInfo dir = new DirectoryInfo(sourceDirName);

        //Make an Array of Directories found on The Device
        DirectoryInfo[] dirs = dir.GetDirectories();

        if (!Directory.Exists(destDirName))
        {
            Directory.CreateDirectory(destDirName);
        }
        //Provide a Device Listing. Here is Where I am Stuck

        for (int i = 1; i < 11; i++)
        {
            string tmp =  destDirName + "\\Device\\ " + i.ToString();
            if ( ! Directory.Exists(tmp)) 
            {
                 Directory.CreateDirectory(tmp);
                 // !!!
                 // here apply your function to copy 
                 // from sourceDirName to directory in tmp variable
                 break;
            }
        }
    }
}

问题2

我敢打赌您无权在 c:\Users\ 中创建目录尝试以管理员身份执行此操作

于 2013-09-20T21:56:20.327 回答
0

好的,所以我弄清楚了我的问题是什么。解决它。2 事情不对...

  1. 这第一个 if 必须只包含一个 (Directory.CreateDirectory(destDirName);) 否则它会弄乱子目录命名
  2. 其次,使用目录信息我创建了一个数组来检查我的目标路径持有什么(我已经拥有设备 1 到无穷大的设备),然后使用计数器添加了一个启用设备列表目录的新设备

    if (!Directory.Exists(destDirName)) {
    Directory.CreateDirectory(destDirName); }

            else
            {
                DirectoryInfo dircheck = new DirectoryInfo(destDirName);
                DirectoryInfo[] dirscheck = dircheck.GetDirectories();
    
                //Start at Device 1
                int count = 1;
    
                Item presents each file in the Destination directory
                foreach (var item in dirscheck)
                {
                    //If the FileName (Lets say Device 1) contained the count which is 1 then increment... Do so until you reach the last index of a device.
                    if (item.Name.Contains(count.ToString()))
                    {
                        count++;
                    }
                }
    
                //Give the Destination directory the name of the last index of the count + 1
                destDirName = holdoriginal + "Device " + count + "\\";
    
                Directory.CreateDirectory(destDirName);
    
            }
    
于 2013-09-23T20:58:03.950 回答