Locating A Folder

SasQuatch

New member
Joined
Oct 14, 2003
Messages
3
Location
Sin City
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?
 
Code:
        Dim strPath As String = Application.ExecutablePath
        /// path of exe including its 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
 
Its worth noting that GetCurrentDirectory will not _always_ be the same as your executable path - if the user has specified a working directory in the shortcut to your application it will be this instead.
 
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:

[VB]
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
[/VB]

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.
 
You shouldnt use Goto or the File commands or On Error, any method should be fine provided it returns what you want:
Code:
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
 
Back
Top