HELP File path confusion in WinForms app

G

GiJeet

Every example I see regarding opening a file uses paths like this:
FileStream inputStream = new FileStream ("C:\\file.txt",
FileMode.Open, FileAccess.Read);

Great if the file is always on the root of the C: drive but what if
that’s not the case?!

If I add a file to a project in my solution I can’t hard code the path
to the file which lives in something like C:\Documents and Settings
\Administrator\My Documents\Visual Studio 2005\Projects….

So how do I access a file using relative paths from the solution? And
if I have a program in one project trys to open a file in another
project how is this accomplished?

For example, I have two projects in my solution A & B. I have a
project reference in A to B. In B I have added an xml file to the
project. It’s in the root of the project. B has a class, BClass that
has a static function that needs to open that file. I call this
function in B from A, eg: BClass.OpenFile(). The file is physically
located in something like C:\Documents and Settings\Administrator\My
Documents\Visual Studio 2005\Projects (which will not exist on the
server this solution gets deployed to) and when A runs it’s the
currently executing program so any Application path will refer to A
not B but the file is in B. How to access that file?

TIA
G
 
G

GiJeet

Finally, for relatively small files one of the most reliable approaches is  
to just include the file as a binary resource in the output assembly (.exe  
or .dll).  Then it's not possible to deploy the assembly without the file,  
since it's actually part of the same file.  You would then retrieve the 
file as a byte stream or array from the resource itself.

Thanks for your reply. It's a small xml file so I'll look into using
binary resources. However, I'll be trying to access a binary resource
in one project exe from another executing project exe so not sure if
that will be a problem...

G
 
G

GiJeet

 It's not really clear from  
your question what the scenario is, as you're distinguishing between "one 
project exe from another executing project", which _might_ imply two  
different processes.  Or it might not.  I can't tell.

I have two projects, one's an exe the other a dll. The dll is the one
that needs the xml file. The exe calls the dll so then the dll needs
to access the xml file. should be very common. Also, the exe project
has a reference set to the dll project so it can access it's methods
at design time.

My concern with an embedded resource file is, I believe every time you
change the xml file, since it’s embedded you need to recompile the dll
project and since the exe has a reference to the dll project, that
existing reference will no longer be valid and you need to remove and
re-add the reference. Just becomes a maintenance issue. Is this
correct?

G
 

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