Click here to Skip to main content
15,886,872 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Is there anyway to generate names not from the txt file by giving the path in the source code but i want the names should be in the source code instead of giving path. This is because if user installed my software, i also have to give them the names files, which i don't want to do that work. so is there anyway to do that headache part?

This is my code:
VB
Private Function RandomLname(ByRef ranLname As String) As String

    Dim userPath As String = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
    Dim lname() As String
    Dim rand As New Random()
    lname = IO.File.ReadAllLines(userPath & "\Desktop\names\lastnames.txt")
    Return lname(rand.Next(0, lname.Length - 1))
    Return ranLname

End Function
Posted
Updated 5-Sep-15 23:53pm
v2
Comments
Suvendu Shekhar Giri 6-Sep-15 5:27am    
..and what you have tried so far?
..and your question is not clear enough too. try explaining a little more.
Wendelius 6-Sep-15 5:28am    
Please clarify the question:
- what names you're talking about
- where do you currently collect them manually
- how do you currently collect them
Member 11927641 6-Sep-15 5:53am    
Sorry for the confusion, question updated
Suvendu Shekhar Giri 6-Sep-15 6:01am    
and what is the problem with this code?
Tomas Takac 6-Sep-15 6:08am    
If the list of names is static i.e. you don't need to update them after installation then include the file as resource in your exe/dll. All then comes in one package with minimal changes to your code.

1 solution

One way to do it is to include the file in your project:
Open your project in the Solution Explorer pane, and right click the project. Select "Add...existing item"
Browse to the file (remembering to select the file type in the dialog) and insert it into your project. This will copy the file to your project folder.
Double click on "My Project", and select "Resources" from the tabs on the right hand side.
Drag your file from Solution Explorer and drop it on the page. This will add it as a resource.
Close the page, and highlight the file in Solution Explorer.
Look at the Properties pane, and change "Build Action" to "Embedded Resource".
You can now add the whole text of the file to your code with:
VB
Dim data As String = My.Resources.NameOfTheFileWithoutTheExtension
And the file will be included as part of your executable.

Do note that this means you can change the file before compiling, but you can't change it's content from inside your application.
 
Share this answer
 
Comments
Member 11927641 6-Sep-15 14:30pm    
Thanks for the solution but also have one issue that, should i have to write "Dim data As String = My.Resources.NameOfTheFileWithoutTheExtension" by replacing this code - lname = IO.File.ReadAllLines(userPath & "\Desktop\names\lastnames.txt") or have to add in other line?

I tried by replacing the code but idk why "lastnames" file is not showing in the list, only i am getting my images in the resources list even i have changed the Embedded Resource..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900