【问题标题】:Load text file using command args使用命令 args 加载文本文件
【发布时间】: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


【解决方案1】:

您应该使用 System.IO.Path 类上的方法

private void Form1_Load(object sender, EventArgs e)
{
    var args = System.Environment.GetCommandLineArgs();
    // using linq here reduces that array count check then extract
    var argPath = args.Skip(1).FirstOrDefault();
    if (!string.IsNullOrEmpty(argPath))
    {
        // Your .LoadFile(...) method requires a full path
        var fullPath = Path.GetFullPath(argPath);
        /* this part isn't needed unless you want to ensure the directory exists... but if it doesn't exist you can't open it anyway
        var dirPath = Path.GetDirectoryName(fullPath);
        if (!Directory.Exists(dirPath))
            Directory.CreateDirectory(dirPath);
        */
        /* this isn't needed since you are using the full path
        Directory.SetCurrentDirectory(dirPath);
        */ 
        addTab();
        getFontCollection();
        setFontSizes();
        getCurrentDocument.LoadFile(fullPath, RichTextBoxStreamType.PlainText);
    }
}

【讨论】:

  • 使用这个,但我仍然收到An unhandled exception of type 'System.NotSupportedException' occurred in mscorlib.dll Additional information: The given path's format is not supported.,所以现在我将继续使用我的版本
  • 调试器说fullPath被设置成什么?
  • 在你的例子中dirPath + '\\' + fiuleName' should be the same as Path.Combine(Path.GetDirectoryName(dirPath), Path.GetFileName(dirPath))`。你的命令行是什么样的?
  • 您是否只向命令行发送相对部分路径?
  • 我一直在发送 C:\users\Giovanni\Desktop\Hello.txt 作为 VS 调试器中的命令行参数。
【解决方案2】:

我设法解决了这个问题!感谢所有离开 cmets 的人,他们真的帮了大忙! 通过搞乱目录设置,我最终得到了以下代码,它正确加载了测试文件!

private void formMain_Load(object sender, EventArgs e)
{
    string[] args = System.Environment.GetCommandLineArgs();
    string dirPath = args[1];
    string fileName = "";
    fileName = Path.GetFileName(dirPath);
    dirPath = dirPath.Substring(3);
    dirPath = Path.GetFullPath(dirPath);
    if (dirPath.Contains('\\')) dirPath = dirPath.Substring(0, dirPath.LastIndexOf('\\'));
    Directory.SetCurrentDirectory(dirPath);
    addTab();
    getFontCollection();
    setFontSizes();
    getCurrentDocument.LoadFile(dirPath + '\\' + fileName, RichTextBoxStreamType.PlainText);
}

【讨论】:

  • 不要这样做。使用Path.Combine 函数。
  • @MatthewWhited 为 dirPath + '\\' + fileName, line?
  • 正确...但您甚至不需要它,因为它与原始 dirPath 相同。看我的例子
  • 这没有意义; dirPath = dirPath.Substring(0, dirPath.LastIndexOf('\\')) 去除尾部反斜杠,但 dirPath + '\\' + fileName 将其添加回来。这段代码与您的问题中显示的不同,这一事实使我怀疑还有其他问题。例如,Directory.SetCurrentDirectory(dirPath) 是从哪里来的?它不在你原来的问题中。
猜你喜欢
  • 2023-03-28
  • 1970-01-01
  • 2012-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-04
  • 2020-09-26
  • 2011-08-03
相关资源
最近更新 更多