C# args 0在此程序中存储什么
此代码在本例中做了什么,C# args 0在此程序中存储什么,c#,C#,此代码在本例中做了什么,args[0]存储了什么?如果我想访问我的目录的路径,但是我不能,尽管我很确定我在同一行上犯了一个错误 string directoryPath = args[0]; 这是我的代码: class Program { static void Main(string[] args) { string directoryPath = args[0]; string[] f
args[0]
存储了什么?如果我想访问我的目录的路径,但是我不能,尽管我很确定我在同一行上犯了一个错误
string directoryPath = args[0];
这是我的代码:
class Program
{
static void Main(string[] args)
{
string directoryPath = args[0];
string[] filesList, filesListTmp;
IFileOperation[] opList = { new FileProcNameAfter10(),
new FileProcEnc(),
new FileProcByExt("jpeg"),
new FileProcByExt("jpg"),
new FileProcByExt("doc"),
new FileProcByExt("pdf"),
new FileProcByExt("djvu")
};
if (Directory.Exists(directoryPath))
{
filesList = Directory.GetFiles(directoryPath);
while (true)
{
Thread.Sleep(500);
filesListTmp = Directory.GetFiles(directoryPath);
foreach (var elem in Enumerable.Except<string>(filesListTmp, filesList))
{
Console.WriteLine(elem);
foreach (var op in opList)
{
if (op.Accept(elem)) op.Process(elem);
}
}
filesList = filesListTmp;
if (Console.KeyAvailable == true && Console.ReadKey(true).Key == ConsoleKey.Escape) break;
}
}
else
{
Console.WriteLine("There is no such directory.");
Console.ReadKey();
}
}
}
类程序
{
静态void Main(字符串[]参数)
{
字符串directoryPath=args[0];
字符串[]filesList,filesListTmp;
IFileOperation[]opList={new FileProcNameAfter10(),
新建FileProcEnc(),
新文件ProcByExt(“jpeg”),
新文件ProcByExt(“jpg”),
新文件ProcByExt(“doc”),
新文件ProcByExt(“pdf”),
新文件ProcByExt(“djvu”)
};
if(Directory.Exists(directoryPath))
{
filesList=Directory.GetFiles(directoryPath);
while(true)
{
睡眠(500);
filelistmp=Directory.GetFiles(directoryPath);
foreach(可枚举中的var元素。除了(filelistmp,filelist))
{
控制台写入线(elem);
foreach(opList中的var op)
{
如果(操作接受(要素))操作过程(要素);
}
}
filesList=filesListTmp;
if(Console.KeyAvailable==true&&Console.ReadKey(true.Key==ConsoleKey.Escape)中断;
}
}
其他的
{
WriteLine(“没有这样的目录”);
Console.ReadKey();
}
}
}
args 0在此程序中存储什么
如果您使用参数从命令行执行exe
,您将获得其中的值
为了进行调试,您也可以通过VisualStudio发送它,请转到“项目属性”、“调试”并在“开始选项”下指定
您需要将命令行参数设置为某个值,以便用某个值填充
arg[0]
。您可以通过项目属性的“调试”选项卡并设置“命令行参数”字段来执行此操作。这将允许您调试应用程序,就像在IDE之外运行时从提示符指定命令行一样。通常,在尝试使用参数之前,您应该始终检查参数是否存在
if (args != null && args.Length > 1)
{
// now you know there's thing in "args[0]"
}
args[0]
包含启动应用程序时传递的第一个命令行参数。args[0]是执行过程中传递的第一个参数,即
C:\Path to your program\program.exe“your directory Path”
好的,
args[0]
表示传递给程序的第一个命令行参数-因此简而言之,我们不知道它在您的情况下代表什么。但是请考虑这个命令行:
MyProgram.exe
MyProgram.exe Hello
MyProgram.exe Hello World
这不会将任何内容传递到args[]
中,因此不会有任何0
索引。现在考虑这个命令行:
MyProgram.exe
MyProgram.exe Hello
MyProgram.exe Hello World
这将把Hello
传递到args[]
中,因此索引0
处的值将是Hello
。现在让我们考虑一下这个命令行:
MyProgram.exe
MyProgram.exe Hello
MyProgram.exe Hello World
这将把Hello
和World
传递到args[]
中,因此索引0
处的值将是Hello
,索引1
处的值将是World
。现在,要记住的另一件事是“
参数,特别是在必须处理路径时:
MyProgram.exe "C:\MyPath\ToSomewhere"
如果我想访问我的目录[…]的路径
如果要获取应用程序的位置,可以尝试
System.Reflection.Assembly.GetExecutingAssembly().Location
请同时查看此问题:无论您传递给ITI什么,如果您不给程序任何参数,则不会仅通过命令行存储任何参数。如果您将文件/文件夹拖到.exe上,第一个参数将是路径。@ashburaczenko,这是因为Windows shell将路径作为命令行参数传递。