我正在尝试使用C#在Visual Studio中创建一个控制台应用程序,以便能够将.txt文件拖放到.exe文件中,并让它在该文件中查找和替换.最后我还想要它然后保存 - 与原始文件名末尾的_unwrapped一样.我是C#的新手,这是我到目前为止所做的.它适用于我放在调试文件夹中的测试文件.如何使用拖动文件进行此操作?我尝试了一些我在谷歌上找到的东西,但是它们没有用,我不理解它们.谢谢!!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
string text = File.ReadAllText("test.txt");
text = text.Replace("~", "~\r\n");
File.WriteAllText("test.txt", text);
}
}
}
Run Code Online (Sandbox Code Playgroud) 我制作了一个可执行文件,我想将任意数量的文件拖到其中。我希望它删除所有这些文件的前 15 个字符。它正在按预期重命名大量文件,但随后会引发错误。
“未处理的异常:System.IndexOutOfRangeException:索引超出数组范围。在 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])
+ …Run Code Online (Sandbox Code Playgroud)