打开一个没有扩展的文件 [英] open a file which doesnt have an extention

查看:77
本文介绍了打开一个没有扩展的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个列表框,其中包含文件夹中的所有文件,其中一些文件没有正确的文件扩展名。通过选择这种类型的文件,我想要一个选项来在记事本中打开文件。我使用下面的代码,但它要求创建一个新文件,而不是在记事本中打开现有文件:



  Imports  System.IO 
Imports System.Net
Imports System.Diagnostics.Process
Imports System.Diagnostics.ProcessStartInfo
Imports System.Diagnostics.ProcessWindowStyle
Imports System.ComponentModel

Public _Default
继承 System.Web.UI.Page
< span class =code-keyword> Dim path As String = ConfigurationManager.AppSettings( filepath
受保护的 Sub Page_Load( ByVal sender 作为 对象 ByVal e 作为 System.EventArgs)句柄 .Load
Dim di As DirectoryInfo
di = DirectoryInfo(路径)
Dim fi() As FileInfo
fi = di .GetFiles()

对于 每个文件作为 FileInfo fi
lstbx_file.Items.Add(file.Name)
下一步
结束 Sub

受保护的 Sub lstbx_file_SelectedIndexChanged(sender As Object ,e As EventArgs)句柄 lstbx_file.SelectedIndexChanged
Dim fname 作为 字符串
fname = path& lstbx_file.SelectedValue
Process.Start( notepad.exe,fname)



结束 Sub
结束

谢谢...

解决方案

您选择的文件的完整路径是错误的。

处理断点(Process.Start(notepad.exe) ,fname)并查看 fname 变量是什么,并仔细检查该文件是否存在。



另外,理想情况下,在创建路径时,您需要使用路径



尝试更换;

 fname = path& lstbx_file.SelectedValue 





with



 fname = Path.Combine(path,lstbx_file.SelectedValue.ToString())





希望这有帮助,

弗雷德里克


Hi,
I have a list box, which has all the files in a folder, in which some of them doesnt have a proper file extention. By selecting this type of file, I want an option to open the file in notepad. I used the below code but its asking for creating a new file instead of opening the existing file in notepad:

Imports System.IO
Imports System.Net
Imports System.Diagnostics.Process
Imports System.Diagnostics.ProcessStartInfo
Imports System.Diagnostics.ProcessWindowStyle
Imports System.ComponentModel

Public Class _Default
    Inherits System.Web.UI.Page
    Dim path As String = ConfigurationManager.AppSettings("filepath")
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim di As DirectoryInfo
        di = New DirectoryInfo(path)
        Dim fi() As FileInfo
        fi = di.GetFiles()

        For Each file As FileInfo In fi
            lstbx_file.Items.Add(file.Name)
        Next
    End Sub

             Protected Sub lstbx_file_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstbx_file.SelectedIndexChanged
        Dim fname As String
        fname = path & lstbx_file.SelectedValue
        Process.Start("notepad.exe", fname)



    End Sub
End Class

Thanks...

解决方案

Your full path to the file selected is wrong.
Put a breakpoint at Process.Start("notepad.exe", fname) and see what the fname variable is, and double check that the file is there.

Also, ideally, when creating paths you'd want to use Path.

Try replacing;

fname = path & lstbx_file.SelectedValue



with

fname = Path.Combine(path, lstbx_file.SelectedValue.ToString())



Hope this helps,
Fredrik


这篇关于打开一个没有扩展的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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