简体   繁体   English

如何在C#中的start选项中提供命令行参数的相对路径

[英]How can I provide relative path to command line arguments in start option in C#

I have one directory folder where we are getting 100 files per day. 我有一个目录文件夹,每天可以获取100个文件。

My program picks the files from the IN folder and puts them in the Out folder after processing them. 我的程序从IN文件夹中选择文件,并在处理它们后将它们放入Out文件夹中。

I am getting the issue that when I am giving the exact location in 我遇到的问题是当我在

Project > Properties > DEBUG > START OPTION > COMMAND LINE ARGUMENTS 项目>属性>调试>开始选项>命令行参数

(ie "C:\\Data\\IN\\File.txt") then the program executes successfully and finds the file, but when I provide the Location like "C:\\Data\\IN" it is not picking any file and is throwing the exception (即“ C:\\ Data \\ IN \\ File.txt”),然后程序成功执行并找到文件,但是当我提供“ C:\\ Data \\ IN”之类的位置时,它没有选择任何文件并抛出了例外

Could not find file 'C:\\Data\\In'. 找不到文件“ C:\\ Data \\ In”。 InnerException is Null. InnerException为Null。

The IN folder is getting 100 different files daily. IN文件夹每天将获取100个不同的文件。 How can I solve this problem? 我怎么解决这个问题?

You need to use the Directory.GetFiles() static class to get a list of files to process. 您需要使用Directory.GetFiles()静态类来获取要处理的文件列表。 The Microsoft Documentation has a useful example. Microsoft文档中有一个有用的示例。

https://msdn.microsoft.com/en-us/library/07wt70x2(v=vs.110).aspx https://msdn.microsoft.com/zh-CN/library/07wt70x2(v=vs.110).aspx

"C:\\Data\\IN" is not a relative path, it is an absolute path to the directory containing the files. “ C:\\ Data \\ IN”不是相对路径,它是包含文件的目录的绝对路径。 You can get the files like this from an absolute directory path: 您可以从绝对目录路径获取如下文件:

string[] files = Directory.GetFiles(@"C:\Data\IN", "*.txt");

If you only know the path of the directory relative to the EXE you can get the absolute path like this: 如果只知道相对于EXE的目录路径,则可以这样获得绝对路径:

string dir = Path.Combine(Application.StartupPath, @"Data\IN");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM