在 C# 中使用通配符获取动态生成文件的字符串?

Using Wildcard in C# to get the string of a dynamic generated file?

这是我在堆栈溢出中的第一个问题。 这是我目前的问题和我希望解决的问题

我有这个每天生成的动态生成文件

"EDIOut5_20170112_063449.csv"

我想把它移到另一个目录。 我目前正在使用 System.IO.File.Move()

我的主要问题是当我尝试调用字符串时,这部分代码是随机生成的

"063449"

所以就这样结束了

 string fileName = "EDIOut"+ dayOfWeekplus + "_" + shortDate + "_" + "063449" + ".csv";

问题是。我可以在 C# 中使用通配符来替换我的代码中随机生成的部分吗?

谢谢!

您可能希望将任务分成 2 个步骤...

  1. 使用 Directory.GetFiles() 查找与特定模式匹配的文件的文件(注意 * 字符是通配符)
  2. 使用 File.Move()
  3. 移动找到的文件

示例代码:

// use wildcard pattern containing *
string pattern = "EDIOut"+ dayOfWeekplus + "_" + shortDate + "_" + "*" + ".csv"
// get list of files matching pattern
string[] files = System.IO.Directory.GetFiles(@"C:\your\path\here\", pattern);
// move found files to new location
for (int i = 0; i < files.Length; i++)
{
    System.IO.File.Move(files[i], @"c:\new\path\" + Path.GetFileName(files[i]));
}