Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi Guys,

I have a question regarding the current path for an application.

 

System.Reflection.Assembly.GetExecutingAssembly.Location

For example this would return:

"C:\Temp\Temp.exe"

 

That code returns the location of the current executable file, now the problem I am having is just finding the applications path. What I am looking to find is just the folder name or:

"C:\Temp\"

 

Is, System.Reflection.Assembly.GetExecutingAssembly.Location the right method to use? If not what would return the current path of the file?

  • Leaders
Posted
       Dim strPath As String = Application.ExecutablePath
       '/// path of exe including it's name ( eg: C:\test.exe )
       Dim folderPath As String = Application.StartupPath
       '/// path minus exe name ( eg: C:\test )
       Dim dir As String = IO.Directory.GetCurrentDirectory
       '/// same as Application.StartupPath

Posted

Thanks for all the help guys, I have another question. All of these ways will work, but for my situation which would be a "good" approach.

 

Here is the function I am using to open the file:

 

   Public Sub Open_File()
       On Error GoTo OpenError
       FileOpen(1, System.IO.Directory.GetCurrentDirectory & "\Dataset.txt", OpenMode.Input)
       Exit Sub

OpenError:
       FileClose(1)
       FileOpen(1, System.IO.Directory.GetCurrentDirectory & "\Dataset.txt", OpenMode.Output)
       If (LOF(1)) = 0 Then
           MsgBox("Dataset.txt was not found, a blank file has been created.", MsgBoxStyle.Information, "Dataset created.")
           FileClose(1)
           Open_File()
       Else
           MsgBox("There was a problem opening the dataset.", MsgBoxStyle.Information, "Open Error")
           FileClose(1)
       End If

   End Sub

 

At execution the Open_File function is called, and this is the only time it is called. I have other subroutines that read the data from the file into an array.

 

Is System.IO.Directory.GetCurrentDirectory the best choice in this situation?

 

The user will not be able to manipulate where the record is loaded from, it will always have to be in the same directory as the .exe.

Posted

You shouldn't use Goto or the File commands or On Error, any method should be fine provided it returns what you want:

Public Sub Open_File()
     Dim File As IO.StreamReader
     Try
         File = New IO.StreamReader(System.IO.Directory.GetCurrentDirectory & "\Dataset.txt")
         'Use File
         File.Close()
     Catch 'Catch any error
       Try
           Dim MakeFile As IO.StreamWriter = New IO.StreamWriter(System.IO.Directory.GetCurrentDirectory & "\Dataset.txt")
           MessageBox.Show("Dataset.txt was not found, a blank file has been created.")
           MakeFile.Close()
        Catch
           MessageBox.Show("File could not be created")
        End Try
     End Try
   End Sub

.Net allows software to be written for any version of Windows and not break like Unmanaged applications unless using Unmanaged procedures like APIs. If your program uses large amounts of memory but releases it when something else needs it, then what's the problem?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...