【发布时间】:2016-07-28 18:46:55
【问题描述】:
我目前正在编写一个文本编辑器程序,但是我遇到了问题。
我试图允许用户通过双击我的程序打开一个文件,这是通过设置默认程序来实现的。有人告诉我,这会将文件路径作为命令参数发送给程序。 我是这样使用它的:
private void formMain_Load(object sender, EventArgs e)
{
string[] args = System.Environment.GetCommandLineArgs();
string filePath = args[1];
addTab();
getFontCollection();
setFontSizes();
getCurrentDocument.Text = (File.ReadAllText(filePath));
}
但是,我一直收到以下错误:
An unhandled exception of type 'System.NotSupportedException' occurred in mscorlib.dll
Additional information: The given path's format is not supported.
如果有人能指导我解决这个问题,我们将不胜感激。 顺便说一句,整个源代码都在Github上,github.com/Criticaldiamonds/asys
编辑
根据 MSDN,第一个参数是程序本身,然后是用户指定的参数。因此,
args[0] = the program
args[1] = "C:\users\Giovanni\Desktop\Hello.txt" (w/o quotes ofc)
由于VS调试器转义字符,args[1]在调试器中的值为“C:\\users\\Giovanni\\Desktop\\Hello.txt”
【问题讨论】:
-
args[1] 中的任何数据都不代表有效路径。
-
能否通过调试查看字符串值是什么?
-
@Ian 值是 C:\\Users\\Giovanni\\Desktop\\Hello.txt 和双反斜杠有关系吗?
-
我猜。路径字符串应该只有一个反斜杠,但前面有一个“@”符号。所以在你的情况下可能
@"C:\Users\Giovanni\Desktop\Hello.txt" -
请edit 您的问题包括
args的所有值。从调试器复制值时请小心,要么转义元字符,如"C:\\Users\\Giovanni\\Desktop\\Hello.txt",要么不转义元字符,如C:\Users\Giovanni\Desktop\Hello.txt。注意到引号和双反斜杠了吗?他们很重要!请不要混淆这些,否则我们无法理解您的问题。
标签: c# winforms command-line-arguments filepath