如何制作自动增量文件夹?

时间:2014-01-10 10:04:51

标签: c# directory auto-increment

我已经尝试过如何制作自动增量文件夹,但编号时有问题,有谁知道问题在哪里?

public void NewFolder()
        {
            try
            {
                string FolderName = Path.Combine(txtOutputFileEn.Text, txtNamaFile.Text);
                tempFolder = FolderName;
                if (!Directory.Exists(tempFolder))
                {
                    Directory.CreateDirectory(tempFolder);
                }
                else if (Directory.Exists(tempFolder))
                {
                    tempFolder = tempFolder + ("001");
                    if (!Directory.Exists(tempFolder))
                    {
                        Directory.CreateDirectory(tempFolder);
                    }
                    else if (Directory.Exists(tempFolder)) 
                    {
                        int x = 1;
                        for (x = 0; x < 50; x++)
                        {
                            string angkaString = tempFolder.Substring(tempFolder.Length - 3);
                            int angka = Convert.ToInt32(angkaString) + x;
                            string angka00 = "00" + angka.ToString();
                            tempFolder = FolderName + angka00.Substring(angka00.Length - 3);
                            if (!Directory.Exists(tempFolder))
                            {
                                Directory.CreateDirectory(tempFolder);
                                return;
                            }
                        }
                    }
                }
                MessageBox.Show(tempFolder);
            }
            catch (IOException ex)
            {
                MessageBox.Show(this, ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
  

这里输出名称文件夹=   folder001,folder002,folder004,folder007,folder11,folder16,folder22。

     

我想创建名称= folder001,folder002,folder003,folder004 .. next

4 个答案:

答案 0 :(得分:2)

我已经清理了你的代码以获得你想要的功能:

public void NewFolder()
{
    try
    {
        string folderName = Path.Combine(txtOutputFileEn.Text, txtNamaFile.Text);
        if (!Directory.Exists(folderName))
        {
            Directory.CreateDirectory(folderName);
        }
        else
        {
            for (int x = 1; x < 50; x++)
            {
                string tempFolder = folderName + x.ToString().PadLeft(3, '0');
                if (!Directory.Exists(tempFolder))
                {
                    Directory.CreateDirectory(tempFolder);
                    MessageBox.Show(tempFolder);
                    break;
                }
            }
        }
    }
    catch (IOException ex)
    {
        MessageBox.Show(this, ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

答案 1 :(得分:2)

你的问题在这里:

                    int angka = Convert.ToInt32(angkaString) + x;

使用此:

                    int angka = Convert.ToInt32(angkaString) + 1;

答案 2 :(得分:1)

试试这个..

  

int angka = Convert.ToInt32(angkaString)+ x;而不是添加 x   在这里,您可以直接添加 1

public void NewFolder()
        {
            try
            {
                string FolderName = Path.Combine(txtOutputFileEn.Text, txtNamaFile.Text);
                tempFolder = FolderName;
                if (!Directory.Exists(tempFolder))
                {
                    Directory.CreateDirectory(tempFolder);
                }
                else if (Directory.Exists(tempFolder))
                {
                    tempFolder = tempFolder + ("001");
                    if (!Directory.Exists(tempFolder))
                    {
                        Directory.CreateDirectory(tempFolder);
                    }
                    else if (Directory.Exists(tempFolder)) 
                    {
                        int x = 1;
                        for (x = 0; x < 50; x++)
                        {
                            string angkaString = tempFolder.Substring(tempFolder.Length - 3);
                            int angka = Convert.ToInt32(angkaString) + 1;
                            string angka00 = "00" + angka.ToString();
                            tempFolder = FolderName + angka00.Substring(angka00.Length - 3);
                            if (!Directory.Exists(tempFolder))
                            {
                                Directory.CreateDirectory(tempFolder);
                                return;
                            }
                        }
                    }
                }
                MessageBox.Show(tempFolder);
            }
            catch (IOException ex)
            {
                MessageBox.Show(this, ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

答案 3 :(得分:-2)

你的代码风格不是很清楚。您应该为方法,变量等使用好的名称。 命名非常重要。

一个好的解决方案是使用.ToString()方法提供的字符串格式。 我修复并简化了代码:

    public void NewFolder()
    {
        try
        {
            string folderName = Path.Combine(txtOutputFileEn.Text, txtNamaFile.Text);

            if (!Directory.Exists(folderName))
            {
                Directory.CreateDirectory(folderName);
            }
            else
            {
                // Creates 50 folder from 001 up to 050
                // This works also if we have folders from 001 up 022 allready. It will extend up to 050
                for (int i = 1; i <= 50; i++)
                {
                    // formatting the string for 3 digits like 001, 002 and so on.
                    string angka00 = i.ToString("D3");

                    var folderWithNumber = folderName + angka00;
                    if (!Directory.Exists(folderWithNumber))
                    {
                        Directory.CreateDirectory(folderWithNumber);
                    }
                }

            }
        }
        catch (IOException ex)
        {
            MessageBox.Show(this, ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }