C#While循环不起作用

时间:2018-08-23 20:00:32

标签: c# .net while-loop

正在使用小型文件移动器的新程序员。不知道为什么我的while语句不起作用。我试图让程序检查文件是否存在于目录中,如果存在,请检查counter++直到出现原始名称,例如2018 Picture(45)等...

private void btnMove_Click(object sender, EventArgs e)
{
    string sourcePath = @"C:\Users\David\Desktop\Personal Pictures & Videos\fromme";
    string destinationPath = @"C:\Users\David\Desktop\Personal Pictures & Videos\practicefolder";

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

    string[] sourcefiles = Directory.GetFiles(sourcePath);
    //looks at each file with its path attached.
    int counter = 1;
    foreach (string sourcefile in sourcefiles)
    {

        if (sourcefile.EndsWith(".jpeg"))
        {
            string destFile = Path.Combine(destinationPath, "2018 Picture" + "(" + counter + ")" + ".jpeg");
            MessageBox.Show(destFile);
            while (Directory.Exists(destFile))
            {
                    counter++;
            }
            //renames and moves files from sourcePath to destinationPath
            File.Move(sourcefile, destFile);

3 个答案:

答案 0 :(得分:3)

仅增加计数器并不会自动更新文件名,您会检查文件名是否存在循环的中断条件。

while(File.Exists(destFile))
{
    counter++;
    destFile = Path.Combine(destinationPath, $"2018 Picture({ counter }).jpeg");
}

我们每次都必须使用递增的计数器更新文件名。

用于字符串连接的$语法是可选的,但可以使文件名组成更加清晰。

此外,Directory.Exists对文件不起作用。如果传递的文件名存在,它将仍然返回false,因为它在文件系统条目上checks for the directory flag

答案 1 :(得分:1)

我发现有几处需要纠正或改进的地方。

private void btnMove_Click(object sender, EventArgs e)
{
    string sourcePath = @"C:\Users\David\Desktop\Personal Pictures & Videos\fromme";
    string destinationPath = @"C:\Users\David\Desktop\Personal Pictures & Videos\practicefolder";

    //no need to check if the path exists. CreateDirectory() already does the right thing
    Directory.CreateDirectory(destinationPath);   

    int counter = 0; 
    var sourcefiles = Directory.EnumerateFiles(sourcePath, "*.jpeg");
    foreach (string sourcefile in sourcefiles)
    {            
        bool tryAgain = true;
        while (tryAgain)
        {
            try 
            {
                counter++;
                destFile = Path.Combine(destinationPath, $"2018 Picture ({ counter }).jpeg");
                File.Move(sourcefile, destFile);
                tryAgain = false;
                MessageBox.Show(destfile);
            }
            catch(IOException ex)
            {   //file I/O is one of the few places where exceptions might be okay for flow control
                tryAgain =  (counter < 10000);
            }
        }
    }
}

答案 2 :(得分:1)

将您的代码放入在循环内创建文件名并在循环外创建File.Move的代码。您还应该设置“计数器”的上限,以免陷入无限循环。如果没有达到限制,则仅执行File.Move。由于您将在每次迭代中更改名称,因此仅应在成功找到新文件名后才显示消息框。

foreach (string sourcefile in sourcefiles)
    {
        if (sourcefile.EndsWith(".jpeg"))
        {
            bool bSuccess = true;
            string destFile = Path.Combine(destinationPath, "2018 Picture" + "(" + counter + ")" + ".jpeg");
            counter = 0;
            while (File.Exists(destFile))
            {
                destFile = Path.Combine(destinationPath, "2018 Picture" + "(" + counter + ")" + ".jpeg");
                counter++;
                if(counter>1000)
                {
                    MessageBox.Show("'Too many tries.' or what ever message you want to use.");                        
                    bSuccess = false;;
                }
            }
            if(bSuccess)
            {
                MessageBox.Show(destFile);
                File.Move(sourcefile, destFile);
            }
      }