Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi All,

I use the below code to open a file from a folder and when i use process.start, i am getting a warning access of shared member,constant member, enum member through an instance, qualifying expression will not be evaluated and the line p.start(fname) gets zigzag underlined in green color throwing the warning.

Could someone please help to remove this,

Thanks in advance!!!

VB
Imports System.IO
Imports System.Net
Imports System.Diagnostics.Process

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
            If (Not IsPostBack) Then
                Dim di As DirectoryInfo = New DirectoryInfo(path)
                Dim fidir As FileInfo() = di.GetFiles()
                Dim fi As FileInfo
                For Each fi In fidir
                    lstbx_file.Items.Add(fi.FullName)
                Next
            End If        
    End Sub

    Protected Sub lstbx_file_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstbx_file.SelectedIndexChanged
            Dim fname As String
            Dim x As New _Default
            fname = System.IO.Path.Combine(x.path, lstbx_file.SelectedValue.ToString())
            Dim p As New Process()
            p.Start(fname)
            p.Close()
            p.Dispose()
    End Sub
End Class
Posted

1 solution

Do not do this;
VB
Dim p As New Process()
p.Start(fname)
p.Close()
p.Dispose()

Do, this instead;
VB
Dim p As Process = Process.Start(fname)
p.WaitForExit() ' Might want to use a timeout here
p.Dispose()


Hope this helps,
Fredrik
 
Share this answer
 
v2
Comments
Sai Janani 29-Aug-13 3:00am    
Hi Fredrik,

Yes, but when a process is started, it needs to be closed, if i use process.start(fname), then how do i close the process that got started.

Thanks for the answer, :-)
Fredrik Bornander 29-Aug-13 3:02am    
You call p.Kill() or wait for the process to finish.
Sai Janani 29-Aug-13 3:03am    
Hey, sorry, I didn't read the solution explanation properly. Thanks, Got and got resolved the warning...

Thanks Fredrik for the prompt help.
Fredrik Bornander 29-Aug-13 3:06am    
Glad I could help.
Sai Janani 29-Aug-13 3:50am    
Hi Fredrik,

When i debug in local its working and file is getting opening, but when i try the url from IE or from any other remote machine its displaying the contents of list box but when i click any file, it goes to postback but does not open the file.

I am using the below code:

Imports System.IO
Imports System.Net
Imports System.Diagnostics.Process

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
Try
If (Not IsPostBack) Then
Dim di As DirectoryInfo = New DirectoryInfo(path)
Dim fidir As FileInfo() = di.GetFiles()
Dim fi As FileInfo
For Each fi In fidir
lstbx_file.Items.Add(fi.Name)
Next
End If
Catch ex As System.IO.IOException
End Try
End Sub

Protected Sub lstbx_file_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstbx_file.SelectedIndexChanged
Try
Dim fname As String
fname = System.IO.Path.Combine(path, lstbx_file.SelectedValue.ToString())
Dim p As Process = Process.Start("notepad.exe", fname)
p.WaitForExit(999999999)
p.Dispose()
Catch ex As Exception

End Try
End Sub
End Class

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900