【发布时间】:2015-09-13 07:41:51
【问题描述】:
我在 Visual Basic 2013 中创建了自己的文本编辑器。我想从应用程序外部用它打开文本文件:通过双击或右键单击从桌面打开它们并打开。
我尝试使用右键单击并打开,但它不起作用,它只是打开了我的应用程序。
如何使我的文本编辑器成为我打开文本文件时使用的编辑器?
【问题讨论】:
标签: vb.net windows windows-explorer
我在 Visual Basic 2013 中创建了自己的文本编辑器。我想从应用程序外部用它打开文本文件:通过双击或右键单击从桌面打开它们并打开。
我尝试使用右键单击并打开,但它不起作用,它只是打开了我的应用程序。
如何使我的文本编辑器成为我打开文本文件时使用的编辑器?
【问题讨论】:
标签: vb.net windows windows-explorer
您必须使用类似Environment.GetCommandLineArgs 的方法。
把它放在你的表单加载事件中:
Dim CommandLineArguments() As String = Environment.GetCommandLineArgs()
If CommandLineArguments.Length >= 2 AndAlso String.IsNullOrEmpty(CommandLineArguments(1)) = False AndAlso IO.File.Exists(CommandLineArguments(1)) Then
Me.TextBox1.Text = IO.File.ReadAllText(CommandLineArguments(1))
End If
这将获取发送到您的应用程序的命令行参数(这是您尝试使用您的应用程序打开的文件的路径)并检查参数是否为现有文件。如果是这样,它会将所有文件的文本读入您的TextBox。
【讨论】:
在表单加载事件中编写此代码。
Private Sub form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim fname As String = Command$()
If Not fname = "" Then
fname = Replace(fname, Chr(34), "")
Dim obj As New System.IO.StreamReader(fname.ToString)
RichTextBox1.Rtf = obj.ReadToEnd
obj.Close()
Me.Text = "Your Application Name " & fname
End If
End Sub
【讨论】: