Solved

VB 2008 Project Folder Description...

Posted on 2010-11-29
10
655 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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

863 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

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now