keeping file with project
keeping file with project
keeping file with project
keeping file with project
keeping file with project
keeping file with project keeping file with project keeping file with project keeping file with project keeping file with project keeping file with project keeping file with project keeping file with project
keeping file with project keeping file with project
keeping file with project
Go Back  Xtreme Visual Basic Talk > > > keeping file with project


Reply
 
Thread Tools Display Modes
  #1  
Old 05-15-2011, 07:09 AM
realolman realolman is offline
Freshman
 
Join Date: Apr 2006
Posts: 46
Default keeping file with project


...VB express... I recently got a new computer, and then realized that a text file that I opened and read from in one of my apps was not there anymore.

I copied the project from my old computer but forgot about the text file.
the project's file pointed to it's folder, and the text file's path was somewhere else

Is there a way I can keep a file with a project when it's compiled, and then how do I set the path in the app so that the file is found when I want to read it?



thank you.
Reply With Quote
  #2  
Old 05-15-2011, 10:28 AM
AtmaWeapon's Avatar
AtmaWeaponkeeping file with project AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

By default, this is what VS does. When you add a file to a project it's copied to the project directory. If you go out of your way to click the drop-down on the "Add" button and choose "Add Link", what happens instead is it references the original path to the file rather than copying it. That means you have to remember to preserve that directory structure if you move the project to a new machine.

So technically, if you just add files like normal you should get what you want.

Or is the question, "It used to be in a specific place on the hard drive, but now that it's not the application can't find it at runtime?" The answer to that is to not hard-code the full path. Sometimes the answer is to let the user find the file via an OpenFileDialog. Other times, you might stash it in AppData then use Environment.GetFolderPath() to get the path to that directory. I can elaborate, but I'm not sure which problem you're having.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #3  
Old 05-15-2011, 11:42 AM
realolman realolman is offline
Freshman
 
Join Date: Apr 2006
Posts: 46
Default

I have a text file that contains data consisting of the names of things and four characteristics of each thing.... the user chooses the item from a drop down list, and the app calculates and displays info based on the four characteristics of the chosen item.

I don't know exactly what you mean by adding files. Are you talking about simply putting it in the solution explorer along with the form(s) and the project?

I did that, but then I don't know how to write the path to the file so it is read by the app when it comes time to read the file during run-time.

I would like to have the file be included with the project wherever it goes and when it is compiled, and I would rather the user didn't even know it existed, but how do I specify the path to the file for when it comes time to open and read the file, by the app., ?

I'd like to keep the file a separate entity so I can modify it when necessary

thanks
Reply With Quote
  #4  
Old 05-17-2011, 09:51 AM
AtmaWeapon's Avatar
AtmaWeaponkeeping file with project AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

OK, it sounds like this is a deployment rather than build issue.

Maybe the file exists on your computer at some path like "C:\data\file.txt". If that's the case, then you need to make your program guarantee one or both of the following:
  1. You always install the file at that location.
  2. The program lets the user tell it where the file exists.
  3. You always install the file to a location you can programmatically determine.

For #1, you can't just send your program to someone else in a .zip file anymore. You need an installer. The not-free versions of VS have a rudimentary install framework but I'm partial to Inno Setup, a freeware install scripting language and compiler. I would urge you to *not* use technique #1 because many people are picky about their hard drives and if all programs picked some random data directory it'd be impossible to find anything on our drives.

For #2, you keep the path to the file in a program setting. If you try to get the file and it fails, you ask the user to tell you where the file is, maybe with an Open File Dialog. However, you may still have to deploy the file with your application and I still recommend Inno Setup for that. Personally I only use #2 in conjunction with #3 or #1. It's a bad experience when a program asks me tons of questions on startup.

For #3, you pick one of the folders Microsoft has asked all applications to use for storing data. Here's a quick way to determine which place to use:
  • If the file is something the user creates and edits, the user's My Documents folder is a good location.
  • If the file is an application data file that the user will rarely have to edit, you should use AppData.
  • If the file belongs in AppData but you want all users to share the same file, you should use Common AppData.
These folders exist in different places on Windows XP, Vista, and Windows 7. Since it'd be annoying to hard-code all of the paths, Microsoft provided the Environment.GetFolderPath() method to return the path to one of these locations. You give it one of the Environment.SpecialFolder values to get the appropriate folder:
  • MyDocuments gives you the path to the user's My Documents folder.
  • ApplicationDatta gives you the path to AppData.
  • CommonApplicationData gives you the path to Common AppData.
If the file has data that the user can't enter, you still need to deploy it to the appropriate location at install. Inno Setup's scripting language has mechanisms for getting the paths to these special directories so you don't have to hard code the paths.

So in short, you have a few choices, but if you want the file to be in a specific location when your program is installed, you have to make an installer that does it.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #5  
Old 05-17-2011, 07:17 PM
realolman realolman is offline
Freshman
 
Join Date: Apr 2006
Posts: 46
Default

Thank you very much for your posts...

I thought it would be a trivial matter... sounds like it might be a bit more involved. The user doesn't need to know anything about the file. If I am understanding this (and I'm not too sure I do ) I think the Environment.GetFolderPath() method sounds like what I want to do . I've never heard of this before

I will look into this and see what I can do.

thanks

edit;
I have been fooling with this for an hour or so and I am afraid I don't get it.

Last edited by realolman; 05-17-2011 at 08:12 PM.
Reply With Quote
  #6  
Old 05-17-2011, 09:24 PM
PlausiblyDamp's Avatar
PlausiblyDampkeeping file with project PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Is there a particular thing you are having problems with? The Environment.GetFolderPath method simply takes one of the values defined by Environment.SpecialFolder as a parameter and returns the corresponding path.

For example if the installer created a folder called MyApp under the ApplicationData folder and within this folder there was a file called settings.ini you could return this with code similar to

Code:
Dim folder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MyApp")
Dim filePath = Path.Combine(folder, "settings.ini")
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #7  
Old 05-18-2011, 09:14 AM
AtmaWeapon's Avatar
AtmaWeaponkeeping file with project AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

I'm as confused as PD. "I don't get it" doesn't help me explain. I already typed up a lot, so it'd be nice if you talked about what you have tried, what you expected it to do, and what it did instead.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #8  
Old 05-19-2011, 05:18 AM
realolman realolman is offline
Freshman
 
Join Date: Apr 2006
Posts: 46
Default

You think YOU'RE confused....

I have a file that contains the names of many items along with characteristics of each item that are numbers

Name of Item one
9.5
10.5
16.0
0.3
Name of Item two
8.6
11.3
15.0
0.0
etc.

In my code I open this file and populate a drop down list with the names. The user selects a name and the code uses the numbers associated with each item to perform calculations and display results.
The file is just a text file that I can modify with word pad if I need to.

I got a new computer, and tried to use the application and I moved the project but had forgot to move the file to the same folder in the new computer as it was in in the old computer so the app couldn't find it. I don't want the user to have to look for it.

I want to make sure the file stays with the app without my having to remember to move it... and along with that... I don't know how to set the path for the app to read the file

Fact is, I was just running the app in debug in the IDE. It had not been compiled.

If I was to just add it to the project explorer, I suppose it would go with the project, but then I don't know to what to set the path in the application...

I don't know how else to put it
Reply With Quote
  #9  
Old 05-19-2011, 07:43 AM
PlausiblyDamp's Avatar
PlausiblyDampkeeping file with project PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

If a normal user was installing this application the should be doing it via an actual installer, this installer would be responsible for creating this supporting file in the correct place.

If the file is not meant to be managed by an end user then the correct place to store it would be as part of either ApplicationData or CommonApplicationData as mentioned by Atma in post #4. If this is user editable then it really should be stored under that user's MyDocuments - all three of these folders can be retrieved using the Environment.GetFolderPath method as mentioned above.

The only other alternative is to keep this file in the same folder as the application, however since XP the program files folder has required administrative rights to perform changes so if the file needs to be edited then this is probably a bad move.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #10  
Old 05-19-2011, 09:31 AM
AtmaWeapon's Avatar
AtmaWeaponkeeping file with project AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

I want to reinforce PD's answer; it can't be said enough.

If your install process is "drag and drop some files" then the way to make sure your data file stays with everything is to make sure you don't forget to drag it. It might be easier to put everything in a .zip file, more for grouping than compression.

If you have an installer, it's up to the installer to keep track of the files. You may have to configure the installer to do so. This is the best way to install an application that has supporting files.

While it's convenient to put a file in the same directory as the program, on Windows Vista and Windows 7 that won't always work well. The decision was made that Program Files and its children should be read-only. This is for security reasons; it's too easy for viruses to infect your programs in XP and earlier. Microsoft created the AppData directory so you could have a safe place to put your writable data files since Program Files isn't allowed. This is less convenient, but more secure.

If you only plan to work on XP, I suppose you can get by without changing. But I assure you one day you're going to move on and you're going to have to learn how to deal with files that need to be installed in AppData.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #11  
Old 05-19-2011, 05:10 PM
realolman realolman is offline
Freshman
 
Join Date: Apr 2006
Posts: 46
Default

Quote:
Originally Posted by PlausiblyDamp View Post
.......

If the file is not meant to be managed by an end user then the correct place to store it would be as part of either ApplicationData or CommonApplicationData as mentioned by Atma in post #4. If this is user editable then it really should be stored under that user's MyDocuments - all three of these folders can be retrieved using the Environment.GetFolderPath method as mentioned above........
Would you be willing to write a few lines of code to show me how to write the path and read a file named "Characteristics.txt" file using the Environment.GetFolderPath method if it is in the CommonApplicationData folder ?



thank you
Reply With Quote
  #12  
Old 05-19-2011, 05:18 PM
PlausiblyDamp's Avatar
PlausiblyDampkeeping file with project PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Code:
Dim filename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Characteristics.txt")
would do the trick, although in practice you would want to put the file in a sub folder of the CommonApplicationData folder e.g.
Code:
Dim filename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "Test\Characteristics.txt")
either way reading the file is then just normal File I/O.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #13  
Old 05-19-2011, 07:25 PM
realolman realolman is offline
Freshman
 
Join Date: Apr 2006
Posts: 46
Default

thank you both . I appreciate all your trouble.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
keeping file with project
keeping file with project
keeping file with project keeping file with project
keeping file with project
keeping file with project
keeping file with project keeping file with project keeping file with project keeping file with project keeping file with project keeping file with project keeping file with project
keeping file with project
keeping file with project
 
keeping file with project
keeping file with project
 
-->