从处理文件中读取字符串 [英] read string from proccessing file

查看:57
本文介绍了从处理文件中读取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个程序.
第一个有一个列表框和一个按钮.
第二个只有一个列表框.
第一个程序将列表框的项目保存为.abc文件中的字符串.
我已将第二个程序与.abc文件相关联,因此,当我双击.abc文件时,它将与第二个程序一起打开.
如何使打开我的.abc文件时,我的第二个程序读取保存在.abc文件中的字符串项目,并将它们作为变量放入列表框中.

I have 2 programs.
The first has 1 listbox and a button.
The second has just a listbox.
The first program saves the listbox''s items as string in an .abc file.
I''ve associated my second program with .abc files so when i double click an .abc file, it opens with my second program.
How to make so that when i open an .abc my second program reads the string items that are saved into the .abc file and put them as a variable into the listbox.

推荐答案

如果您致电
在主表单的load事件中,
If you call
string[] args = Environment.GetCommandLineArgs();

,您将收到文件名.



"它表示字符串是类类型,不能用作表达式.
我需要将文件中的项目作为字符串使用,以将它们与变量一起使用.
能给我一个示例代码吗,请问我真的是编程新手!! :)谢谢!"


in the load event of your main form, you will receive the file name.



"it says string is a class type and cannot be used as an expression.
i need the items that are into the file as string to use them with a variable.
can you give me a sample code please I am really new in programming PLEASE!! :) thanks!"


string[] args = Environment.GetCommandLineArgs();
foreach (string filename in args)
    {
    if (File.Exists(filename))
        {
        // Pick one of these as appropriate for your app.
        string text = File.ReadAllText(filename);
        string[] textAsLines = File.ReadAllLines(filename);
        // Handle the file content
        ...
        }
    }


您的程序将不会处理命令行,除非您编写分析和检查命令行的代码. :-)在您的情况下,程序应尝试将命令行参数解释为文件名,并尝试加载文件.您可以按照格里夫(Griff)的建议进行操作.

对于易于使用且全面的命令行解析器,我也可以推荐我自己的作品基于枚举的命令行实用程序 [ ^ ].

在本文中,您还可以找到我推荐的另一个库,该库较难使用,但更通用.

—SA
Your program will not process command line unless you write the code which parses and examines the command line. :-) In your case, the program should try to interpret the command line parameter as file names(s) and try to load the file(s). You could do it the way Griff advices.

For an easy-to-use and comprehensive command line parser I could also recommend my own work Enumeration-based Command Line Utility[^].

In this article you can also find another library I recommend, harder to use but more universal.

—SA


这篇关于从处理文件中读取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆