SasQuatch Posted October 14, 2003 Posted October 14, 2003 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? Quote
*Experts* Volte Posted October 14, 2003 *Experts* Posted October 14, 2003 Try this:System.IO.Directory.GetParent(pathToFile) Quote
SasQuatch Posted October 14, 2003 Author Posted October 14, 2003 Thanks man, that worked and so does: System.IO.Directory.GetCurrentDirectory Quote
Leaders dynamic_sysop Posted October 14, 2003 Leaders Posted October 14, 2003 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 Quote
*Gurus* divil Posted October 14, 2003 *Gurus* Posted October 14, 2003 It's 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. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
SasQuatch Posted October 15, 2003 Author Posted October 15, 2003 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. Quote
AndreRyan Posted October 15, 2003 Posted October 15, 2003 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 Quote .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?
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.