cra*_*str 17 .net c# vb.net wildcard
我在过去使用setargv.obj链接扩展通配符参数用于许多C和C++应用程序,但我找不到.net应用程序的任何类似提及.
是否有一种标准方法可以让您的应用程序的命令行参数自动扩展通配符?(即将args参数中的一个条目的*.doc扩展为与该通配符匹配的所有条目).
PS我已经使用Directory.GetFiles()为我当前的小项目攻击了一些东西,但它没有覆盖带路径的通配符(还有),没有自定义代码就可以做到这一点.
更新:这是我粗略的黑客,为了说明.它需要拆分GetFiles的路径和名称参数,但这是一般的想法.将setargv.obj链接到C或C++应用程序基本上会执行所有通配符扩展,使用户只能遍历argv数组.
static void Main(string[] args)
{
foreach (String argString in args)
{
// Split into path and wildcard
int lastBackslashPos = argString.LastIndexOf('\\') + 1;
path = argString.Substring(0, lastBackslashPos);
filenameOnly = argString.Substring(lastBackslashPos,
argString.Length - lastBackslashPos);
String[] fileList = System.IO.Directory.GetFiles(path, filenameOnly);
foreach (String fileName in fileList)
{
//do things for each file
}
}
}
Run Code Online (Sandbox Code Playgroud)
在这里我们粗暴的黑客.我喜欢它是递归的.在遇到Windows通配符的缺点后,我可能会决定使用正则表达式,而不是让GetFiles()为我做.
using System.IO;
public static string[] ExpandFilePaths(string[] args)
{
var fileList = new List<string>();
foreach (var arg in args)
{
var substitutedArg = System.Environment.ExpandEnvironmentVariables(arg);
var dirPart = Path.GetDirectoryName(substitutedArg);
if (dirPart.Length == 0)
dirPart = ".";
var filePart = Path.GetFileName(substitutedArg);
foreach (var filepath in Directory.GetFiles(dirPart, filePart))
fileList.Add(filepath);
}
return fileList.ToArray();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8810 次 |
| 最近记录: |