Question about file location

A

Anthony Papillion

Hello again everyone,

I'm developing an application that depends on an external data file. I've
created a new folder within the project and then added the data file to it.
I called the folder resources.

Now, I'm trying to create a StreamReader to use the file. This is the code
I'm using:

Try
Dim reader as New
StreamReader("System.AppDomain.CurrentDomain.BaseDirectory() &
"resources\datafile.dat", true)
Catch ex As Exception
MsgBox("Could not open file")
End Try

Now, you would think that this would work, right (maybe not)? Every time I
run the program, my catch() block is activated. So, I put in a

MsgBox(System.AppDomain.CurrentDomain.BaseDirectory())

at the beginning of the Form_Load() event. I compliled, published, and
installed the application. When I start the application, the path displayed
in the MsgBox is something like
"C:\Users\Anthony\AppData\Local\Apps\2.0\SOME_LONG_STRING\datafile.ion_SOME_LONG_STRING"

WTF?

Shouldn't this be in C:\PROGRAM FILES\MY APP?

Can anyone help me sort this?

Thanks!
Anthony
 
A

Armin Zingler

Anthony said:
Hello again everyone,

I'm developing an application that depends on an external data file. I've
created a new folder within the project and then added the data file to it.
I called the folder resources.

Now, I'm trying to create a StreamReader to use the file. This is the code
I'm using:

Try
Dim reader as New
StreamReader("System.AppDomain.CurrentDomain.BaseDirectory() &
"resources\datafile.dat", true)
Catch ex As Exception
MsgBox("Could not open file")
End Try

Show us the real code. :) I count 3 quotation marks in the 1st line. Number
must be even.

In general, use Sytem.IO.Path.Combine to build pathes. No backslash twiddling
anymore. However, I don't have an answer to why you get that path. Here, BaseDirectory
returns the path of the directory containing the Exe file. Haven't use the property
before; I've always extracted the path from Application.ExecutablePath. Maybe
you can give it a try also.
 
Á

Áíôþíçò

Use the Application.StartupPath property instead.


óôéò 01/24/2010 01:32, O/H Anthony Papillion Ýãñáøå:
 
F

Family Tree Mike

Hello again everyone,

I'm developing an application that depends on an external data file.
I've created a new folder within the project and then added the data
file to it. I called the folder resources.

Now, I'm trying to create a StreamReader to use the file. This is the
code I'm using:

Try
Dim reader as New
StreamReader("System.AppDomain.CurrentDomain.BaseDirectory() &
"resources\datafile.dat", true)
Catch ex As Exception
MsgBox("Could not open file")
End Try

Now, you would think that this would work, right (maybe not)? Every time
I run the program, my catch() block is activated. So, I put in a

MsgBox(System.AppDomain.CurrentDomain.BaseDirectory())

at the beginning of the Form_Load() event. I compliled, published, and
installed the application. When I start the application, the path
displayed in the MsgBox is something like
"C:\Users\Anthony\AppData\Local\Apps\2.0\SOME_LONG_STRING\datafile.ion_SOME_LONG_STRING"


WTF?

Shouldn't this be in C:\PROGRAM FILES\MY APP?

Can anyone help me sort this?

Thanks!
Anthony

You can use Path.GetDirectoryName(Application.ExecutablePath), or
Application.StartPath, unless your shortcut specifically starts in a
different folder.

The documentation on MSDN states why the method you chose points elsewhere.
 
A

Alex Clark

Solve the problem entirely by not putting data files in your app's directory
(or subdirectories) in the first place. Instead put them in the appropriate
user/machine app data folder provided for you by Windows. You'll find this
easier to access and it will also play nice with Vista's UAC system in the
future.
 
F

Family Tree Mike

Solve the problem entirely by not putting data files in your app's directory
(or subdirectories) in the first place. Instead put them in the appropriate
user/machine app data folder provided for you by Windows. You'll find this
easier to access and it will also play nice with Vista's UAC system in the
future.

The UAC only barks at write access requests to such files. The OP is
opening a stream reader, so that should be fine. Totally appropriate
advice for writable files though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top