将多个文件拖放到可执行 C# 上

Drop multiple files onto executable C#

我制作了一个可执行文件,我想在其中拖放任意数量的文件。我希望它删除所有这些文件的前 15 个字符。它正在按预期重命名大量文件,但随后会抛出错误。

"Unhandeled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array. at RemoveTimeStampMultipleFiles.Program.Main(String[] args)"

当我拖动 2 个文件时,是否正在寻找第三个文件?

还有没有办法允许无限文件而不是最多 99 个文件?

using System;
using System.IO;

namespace RenameVersion2
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length == 0)
                return; // return if no file was dragged onto exe


            for (int i = 0; i < 99; i++)
            {
                if (args[i].Length == 0)
                    System.Environment.Exit(0);

                string MyString = Path.GetFileNameWithoutExtension(args[i]);
                String NewFileName = MyString.Remove(0, 15);


                string path = Path.GetDirectoryName(args[i])
                   + Path.DirectorySeparatorChar
                   + MyString
                   + Path.GetExtension(args[i]);

                string newPath = Path.GetDirectoryName(args[i])
                   + Path.DirectorySeparatorChar
                   + NewFileName
                   + Path.GetExtension(args[i]);


                File.Move(path, newPath);
            }


            System.Environment.Exit(0);
        }
    }
}

您的应用程序似乎正好需要 99 个参数,因为您的 i 循环从 0 到 98。您的错误发生是因为在处理了很多参数之后,它尝试前进到下一个不存在的参数.试试这个:

using System;
using System.IO;

namespace RenameVersion2
{
    class Program
    {
        static void Main(string[] args)
        {
            foreach (var arg in args)
            {
                if (arg.Length == 0)
                    System.Environment.Exit(0);

                string MyString = Path.GetFileNameWithoutExtension(arg);
                String NewFileName = MyString.Remove(0, 15);


                string path = Path.GetDirectoryName(arg)
                   + Path.DirectorySeparatorChar
                   + MyString
                   + Path.GetExtension(arg);

                string newPath = Path.GetDirectoryName(arg)
                   + Path.DirectorySeparatorChar
                   + NewFileName
                   + Path.GetExtension(arg);


                File.Move(path, newPath);
            }


        }
    }
}

您需要修复 for-loop 以便它遍历可用索引而不是超出范围。

for (int i = 0; i < args.Length; i++)
            {
                if (args[i].Length == 0)
                    System.Environment.Exit(0);

                string MyString = Path.GetFileNameWithoutExtension(args[i]);
                String NewFileName = MyString.Remove(0, 15);


                string path = Path.GetDirectoryName(args[i])
                   + Path.DirectorySeparatorChar
                   + MyString
                   + Path.GetExtension(args[i]);

                string newPath = Path.GetDirectoryName(args[i])
                   + Path.DirectorySeparatorChar
                   + NewFileName
                   + Path.GetExtension(args[i]);


                File.Move(path, newPath);
            }

区别就在这里

for (int i = 0; i < args.Length; i++)

for (int i = 0; i < 99; i++)