如何使用c#应用程序打开文件 [英] how to open a file using the c# application

查看:227
本文介绍了如何使用c#应用程序打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的记事本应用程序。

我已经通过为该应用程序创建安装文件将其部署在我的系统中。

但是,只有一个问题是



一般我们以这种方式打开文件





右击a文件 - >打开 - >选择我们想要打开文件的程序。





然后该文件将加载到应用程序中提供的richtextBox字段中。



任何人都可以告诉我在打开文件时我必须添加什么代码才能加载文件?????

I have created a simple notepad application.
I have deployed it in my system by creating setup file for that app.
But , only one problem is that

generally we open a file in this way


right click a file -> open with -> choose the program with which we want to open the file.


then the file will be loaded into the richtextBox field provided in the app.

Can any one suggest me what is the code i have to add to get the file loaded as we open the file?????

推荐答案

这是 System.Windows.Forms.RichTextBox.LoadFile http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.loadfile.aspx [ ^ ]。



-SA


System.Windows.Forms.RichTextBox.LoadFile(FileName, RichTextBoxStreamType);


要使这个右键单击 - 打开方法工作,你需要处理程序的命令行参数。如果操作系统已经打算打开文件,操作系统将会调用你的可执行文件和

2.已经添加了路径到该文件作为参数。



使用 Environment.GetCommandLineArgs() [ ^ ]获取调用可执行文件的所有参数的方法,并检查第一个是否是可以打开的文件。如果是这样,那么就像用户在应用程序中单击文件 - 打开 - [文件名]后那样打开它。
To make this right-click-open-with-approach work, you need to process command-line parameters to your program. If it has been called by the operating system with the intent to open a file, the OS will
1. have called your executable and
2. have appended the path to the file as parameter.

Use the Environment.GetCommandLineArgs()[^] method to get all the arguments that your executable has been called with and check if the first one is a file that you can open. If so, then open it just like you would do after user clicks File - Open - [filename] from within your application.


这篇关于如何使用c#应用程序打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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