Solved

VB 2008 Project Folder Description...

Posted on 2010-11-29
10
657 Views
Last Modified: 2012-05-10
I have two files in .../bin/Debug folder as shown on the attached image. The original project folder (in this case 'Eghtebas_Mike Ch 9 Problem 3') could be anywhere (in C: drive, on desktop not necessarily in VB2008 defaule folder).

SampleData.txt
ScoreList.txt

Once in a while user may want to reset the data in ScoreList.txt to what is in SampleData.txt.

Q1: What is the best way to do this (for example copy SampleData.txt as SampleData_copy.txt and then delete ScoreList.txt and at last rename SampleData_copy.txt as ScoreList.txt)? What is the code to do this because bin/Debugh could be anywhere as discussed before.

Q2: What is the function of each folder shown on the attached image? I understand this could be a lenghty description; therefore, identifying the major folders will do. Also, any link to read more about the folder structure will be helpful.

Thank you.



 VB2008 Project Folder
0
Comment
Question by:Mike Eghtebas
  • 5
  • 4
10 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34237026
1) You could do that. You can use Application.StartupPath to give you the folder where your application lives.

2) "bin" contains the binaries from your project, one set from teh Debug build and one from the Release build.
    "obj" contains temporary files used by the compiler while building the project. Again there are versions for Debug and Release.
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 34239894
re:> 1) You could do that.

I need the code. Also, if the project folder in my desktop, running it on your desktop, how would it know its location?

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 34241018
"Once in a while user may want to reset the data in ScoreList.txt to what is in SampleData.txt."

You could embed SampleData.txt in your exe so it doesn't need to be external.  Then you can simply read it and overwrite the contents of ScoreList.txt.

To add the text file as an embedded resource:
(1) Project --> Add Existing Item.
(2) Change Filter to All Files, browser to your text file, and double click it.
(3) Select the resource in the Solution Explorer Pane (top right by default).
(4) Down below in the Properties Pane, change the Build Action to Embedded Resource.
*Note that when accessing this resource by name it is CaSe-SeNsItIvE so make it match exactly as it appears in your solution explorer listing.

You can use "My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData" to hold your data file.

If the data file doesn't exist at form load you can simply create it using the data from the embedded resource.

Here's where the GetResourceContent() function came from:
http://www.devx.com/vb2themax/Tip/19589

Simple example:
Imports System.IO
Public Class Form1

    Dim DataFile As String = My.Computer.FileSystem.CombinePath(My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData, "ScoreList.txt")

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If Not My.Computer.FileSystem.FileExists(DataFile) Then
            ResetDataFile()
        End If
    End Sub

    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ResetDataFile()

        MessageBox.Show(DataFile)
        Process.Start(DataFile)
    End Sub

    Private Sub ResetDataFile()
        Dim contents As String = GetResourceContent("SampleData.txt")
        My.Computer.FileSystem.WriteAllText(DataFile, contents, False)
    End Sub

    Function GetResourceContent(ByVal filename As String) As String
        ' get the current assembly
        Dim asm As System.Reflection.[Assembly] = _
            System.Reflection.[Assembly].GetExecutingAssembly()
        ' resources are named using a fully qualified name
        Dim strm As System.IO.Stream = asm.GetManifestResourceStream(asm.GetName() _
            .Name + "." + filename)
        ' read the contents of the embedded file
        Dim reader As New System.IO.StreamReader(strm)
        GetResourceContent = reader.ReadToEnd()
        reader.Close()
    End Function

End Class

Open in new window

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 34242367
1. re:> CaSe-SeNsItIvE

Is this short like FYI (for For Your Information)? Or, it is a name I will be using it in my codes?

2. re:> My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData
Because it is intended to keep it in bin/Debug, is the following path correct?
Using ScoreFile As New StreamReader(My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData & "\bin/Debug\ScoreList.txt")    or perhaps

Using ScoreFile As New StreamReader(path + "\bin\Debug\ScoreList.txt")

3. re:> If the data file doesn't exist at form load you can simply create it using the data from the embedded resource.

In my code, while executing (for exampe):

Using ScoreFile As New StreamReader(path + "\bin/Debug\ScoreList.txt")

If the txt file is not found, what the error trapping needs to be and how can I copy and rename the embeded SampleData.txt to bin\Debug\ScoreList.txt?

Sorry, there are lots of questions here. Please let me know if I need to break them to new questions.

Mike
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 34242398
I guess 3 question is answered by your code at the end of your last post.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 34242469
#1 Case Sensitive means that you have to match the case EXACTLY for it to work.  This is one of the few instances in VB.Net where case actually matters.  For example, if the embedded text file appeared as "SomeFile.txt" in Solution Explorer, then you'd have to pass "SomeFile.txt" to GetResourceContent() exactly as it appears.  If you passed "somefile.txt" then the embedded resource would not be found since the case doesn't match.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 34242518
#2 The directory isn't easily accessible so I wouldn't bother with forcing a sub-tree structure unless there was a very good reason for it.  If the user needs to access the file then I'd let them pick the location using a SaveFileDialog().

Try this out to see where the folder is:

    Dim DataFile As String = My.Computer.FileSystem.CombinePath(My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData, "ScoreList.txt")

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MessageBox.Show(DataFile)
    End Sub

If you do want to keep the subfolders then you'd also have to check for the existence of the folders and create them before you go trying to create the file.
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 34243075
Hi Idle_Mind,

This wonderful code reproduces sample data from SampleData.txt so the user could continue her/his work. However, after the session ends and the application is closed, all of the new data will be lost because we have not created a physical copy of the working file as bin\Debug\ScoreList.txt.

Also, there is an error as you can see on the attache dimage along with 3 questions for your comment.

Thank you,

Mike
VB2008ProjectFolder-1.png
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 34249449
Hadn't gotten back to this last issue yet...you figured it all out?
0
 
LVL 33

Author Comment

by:Mike Eghtebas
ID: 34249785
It seems, I need to do some reading to get my arms around the concept. Meanwhile I want to get this small project done using the method discussed (a new question):

http://www.experts-exchange.com/Programming/Languages/.NET/Q_26650472.html

Thank you for the follow-uo.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question