13 Replies - 3391 Views - Last Post: 09 December 2012 - 05:35 PM Rate Topic: -----

#1 HeBTeMy   User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Store txt files

Posted 08 December 2012 - 06:39 PM

Hi, i'm making an application that needs to store files on it, how can i do it?

Let me explain, i need it can store strings in the application(not in the computer) that when i close and open again it shows the strings there.. with option to save them to txt.
I don't know how to store them in the app itself, anyone knows?

Thanks btw
Is This A Good Question/Topic? 0
  • +

Replies To: Store txt files

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14092
  • View blog
  • Posts: 56,460
  • Joined: 12-June 08

Re: Store txt files

Posted 08 December 2012 - 06:46 PM

You don't really _store_ anything in the app. You have the 'my.settings' you can add things to, but in reality that's just microsoft writing the values to a specific file in the local directory. If you saved a string in the 'my.settings' and then copied that .exe to another computer that value you saved will not port.

You can write files to a file server.. that would keep them in a safe location..
Was This Post Helpful? 0
  • +
  • -

#3 HeBTeMy   User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: Store txt files

Posted 08 December 2012 - 07:05 PM

So i can't add a string "stored" in the app? Don't care wich way it is, i can't do it for opening/saving them later? Or simple text? Not the file itself?
And what do you mean to a "file server"? Stored in an account like ftp online?
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14092
  • View blog
  • Posts: 56,460
  • Joined: 12-June 08

Re: Store txt files

Posted 08 December 2012 - 07:12 PM

Quote

So i can't add a string "stored" in the app?

Correct.. your app is compiled when you have an .exe, right? You adding bits to that would require a recompile.

Quote

Don't care wich way it is, i can't do it for opening/saving them later? Or simple text? Not the file itself?

I don't know what this means.

Quote

And what do you mean to a "file server"? Stored in an account like ftp online?

sure.. an ftp site, some sort of webservice to a database, etc.
Was This Post Helpful? 1
  • +
  • -

#5 HeBTeMy   User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: Store txt files

Posted 08 December 2012 - 07:37 PM

I was trying to say and asking if don't exists any other way to save it... i guess not.

But if it stores the file on disk, or if i make it installable (newbie for that either) using the file(string;txt) stored in disk that the program could have "ready for view" automatycally when it opens or something, that i know it's possible, but i'd have to install it first and then find a way to open it (or them if there were multiple files) automatically...

Thanks for the help.
Was This Post Helpful? 0
  • +
  • -

#6 HeBTeMy   User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: Store txt files

Posted 09 December 2012 - 09:05 AM

I stumble in to other thing, how i can delete a specific word in a textbox?

Like the textbox has "thing1 thing2", how i can delete the "thing2" leaving the "thing1" there?

i've tried many things but without success.

any help pls
Was This Post Helpful? 0
  • +
  • -

#7 lar3ry   User is offline

  • Coding Geezer
  • member icon

Reputation: 314
  • View blog
  • Posts: 1,296
  • Joined: 12-September 12

Re: Store txt files

Posted 09 December 2012 - 09:33 AM

View PostHeBTeMy, on 08 December 2012 - 08:37 PM, said:

I was trying to say and asking if don't exists any other way to save it... i guess not.

But if it stores the file on disk, or if i make it installable (newbie for that either) using the file(string;txt) stored in disk that the program could have "ready for view" automatycally when it opens or something, that i know it's possible, but i'd have to install it first and then find a way to open it (or them if there were multiple files) automatically...

It depends entirely on what you want to store. Yes, you can have strings in a resource, or defined with code, but they are compiled in when you make the .exe, and can't be changed by the user. This type of string would usually be for things like prompts for MessageBoxes, ot titles for forms, and so on.

As was pointed out to you, My.Settings strings are actually saved in a file by the code implemented in the My.Settings commands. This is not a bad thing, really, because the settings are available to the program. The difficulty lies with distributing an initial collection of settings with the .exe, and again, it depends on what it is you are using the settings for. One example might be a set of defaults for things like the size of the form, its position on the screen, location of data files, initial states for RadioButtons or CheckBoxes, and so on. If this is the case, you can generate a My.Settings set of defaults if a My.Settings does not already exist. Then, when a user modifies the default settings, perhaps via a configuration/options/preference form, you can save the new settings as a new default.

The other option is the same as My.Settings, except that you hgave more control over where the file containing the settings resides, as well as more control over the way the contents are stored. This might be better for your purposes, or not, so the choice is up to you. As for where the file might be stored, AppPath is always OK, or you might want to use some of the special directories (user\AppData\<your program name>\<config file name>.txt) or anywhere else the OS will allow a user to write to.

If the set of strings is something more than options/prefs, you might want to create a file containing whatever you want as text, place it in AppPath or CurDir(). AppPath is always where the .exe is saved on disk, but CurDir may be set to somewhere else by a shortcut, as "Start in". If you do this, and if the user needs the strings the first time she runs the program, you will need to include the file in the Install. As for loading it up, if you do that in your Form_Load subroutine, the user will have that info already in the program before he can start interacting with it.

Hope this helps clarify things. If you have any other specific concerns, or uses for the strings that I haven't thought of or taken into account, feel free to ask more questions.
Was This Post Helpful? 1
  • +
  • -

#8 lar3ry   User is offline

  • Coding Geezer
  • member icon

Reputation: 314
  • View blog
  • Posts: 1,296
  • Joined: 12-September 12

Re: Store txt files

Posted 09 December 2012 - 09:40 AM

View PostHeBTeMy, on 09 December 2012 - 10:05 AM, said:

Like the textbox has "thing1 thing2", how i can delete the "thing2" leaving the "thing1" there?

Check out Replace, as in:
    TextBox1.Text = textBox1.Text.Replace("thing2","")


Was This Post Helpful? 1
  • +
  • -

#9 HeBTeMy   User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: Store txt files

Posted 09 December 2012 - 10:22 AM

Thanks a million times !! Was going crazy searching for days...

about the txt files, i store them in my application.startuppath, but how can i view them automatically when application starts? How can i tell my program to open every txt file on that directory? Or put it together in the program that i can view what's inside.

That's possible?

It's my weekly reports in txt.
Was This Post Helpful? 0
  • +
  • -

#10 lar3ry   User is offline

  • Coding Geezer
  • member icon

Reputation: 314
  • View blog
  • Posts: 1,296
  • Joined: 12-September 12

Re: Store txt files

Posted 09 December 2012 - 12:17 PM

View PostHeBTeMy, on 09 December 2012 - 11:22 AM, said:

about the txt files, i store them in my application.startuppath, but how can i view them automatically when application starts? How can i tell my program to open every txt file on that directory? Or put it together in the program that i can view what's inside.

That's possible?

It's my weekly reports in txt.

Sure it's possible. You should be storing them with file names that contain strings that are recognizable, so that you can differentiate those files from any other files that might be in the directory. For example, you would want to store them as "Weekly Report nnnnnn.txt", where "nnnnnn" is a date or other unique identifier. In this way, you can gather all the relevalnt file names in your Form_Load subroutine, and store the names in an array or list.

You obviously don't read them all at once (unless you have many eyes and a brain that can simultaneously handle many different texts at once), so there is no need to display them all at once, and probably no need to open them all at once, because opening them as needed and closing them when no longer needed is a much more efficient use of your computer's resources and cycles.

So, the UI to look at the files can take many forms. A ListBox with all the filenames, or with just the dates, might be one way, but I'm sure there are hundreds of ways to make the files easily selectable and quick to load.

As for your Form_Load subroutine, have a look at the Directory Class, and in particular, Directory.EnumerateFiles(path,pattern), which will return a collection of file names that you can use to get files based on those names and populate a ListBox or similar.
Was This Post Helpful? 1
  • +
  • -

#11 lucky3   User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 233
  • View blog
  • Posts: 770
  • Joined: 19-October 11

Re: Store txt files

Posted 09 December 2012 - 01:02 PM

You can also store the files with your custom file type identifier (like .heb instead of .txt), and simply get just those files. Or you save your file paths to My.Resurces as strings, and read them when the app loads.
Was This Post Helpful? 0
  • +
  • -

#12 HeBTeMy   User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: Store txt files

Posted 09 December 2012 - 03:36 PM

View Postlar3ry, on 09 December 2012 - 12:17 PM, said:

View PostHeBTeMy, on 09 December 2012 - 11:22 AM, said:

about the txt files, i store them in my application.startuppath, but how can i view them automatically when application starts? How can i tell my program to open every txt file on that directory? Or put it together in the program that i can view what's inside.

That's possible?

It's my weekly reports in txt.

Sure it's possible. You should be storing them with file names that contain strings that are recognizable, so that you can differentiate those files from any other files that might be in the directory. For example, you would want to store them as "Weekly Report nnnnnn.txt", where "nnnnnn" is a date or other unique identifier. In this way, you can gather all the relevalnt file names in your Form_Load subroutine, and store the names in an array or list.

You obviously don't read them all at once (unless you have many eyes and a brain that can simultaneously handle many different texts at once), so there is no need to display them all at once, and probably no need to open them all at once, because opening them as needed and closing them when no longer needed is a much more efficient use of your computer's resources and cycles.

So, the UI to look at the files can take many forms. A ListBox with all the filenames, or with just the dates, might be one way, but I'm sure there are hundreds of ways to make the files easily selectable and quick to load.

As for your Form_Load subroutine, have a look at the Directory Class, and in particular, Directory.EnumerateFiles(path,pattern), which will return a collection of file names that you can use to get files based on those names and populate a ListBox or similar.


I couldn't find Directory.EnumerateFiles(path,pattern) ...
i have a listbox that shows the names of the .txt files that it has in that directory, but i want when i select in listbox the text inside that selected file shows up on a richtextbox, and i can't figure how i get the current selecteditem's name...

i have this:
  Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim asd As String = ListBox1.SelectedItem
        RichTextBox1.LoadFile(Application.StartupPath & asd, RichTextBoxStreamType.PlainText)
    End Sub



and i don't understand why it doesn't load the txt when selected...
The only way i can load something with that code is if i put
RichTextBox1.LoadFile(Application.ExecutablePath, RichTextBoxStreamType.PlainText)
   


and it loads me the .exe of the app itself...
Was This Post Helpful? 0
  • +
  • -

#13 lar3ry   User is offline

  • Coding Geezer
  • member icon

Reputation: 314
  • View blog
  • Posts: 1,296
  • Joined: 12-September 12

Re: Store txt files

Posted 09 December 2012 - 04:43 PM

View PostHeBTeMy, on 09 December 2012 - 04:36 PM, said:

I couldn't find Directory.EnumerateFiles(path,pattern) ...
i have a listbox that shows the names of the .txt files that it has in that directory, but i want when i select in listbox the text inside that selected file shows up on a richtextbox, and i can't figure how i get the current selecteditem's name...

i have this:
  Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim asd As String = ListBox1.SelectedItem
        RichTextBox1.LoadFile(Application.StartupPath & asd, RichTextBoxStreamType.PlainText)
    End Sub



and i don't understand why it doesn't load the txt when selected...

Place a breakpoint on the line RichTtextBox.Load ... and run your program. When it stops, check the conents of Application.StartupPath, and you'll find that it has no trailing "\", so you'll need to add it between the path and the filename.

Quote

The only way i can load something with that code is if i put
RichTextBox1.LoadFile(Application.ExecutablePath, RichTextBoxStreamType.PlainText)
   

and it loads me the .exe of the app itself...

The reason for that is that Application.ExecutablePath includes the filename of the executable.

EnumerateFiles() is part of the Directory Class, but if you already have the filenames in the ListBox, you won't need it. I'd be interested to know how you got them in there.

Documentation for it is on MSDN
Was This Post Helpful? 1
  • +
  • -

#14 HeBTeMy   User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 299
  • Joined: 05-October 12

Re: Store txt files

Posted 09 December 2012 - 05:35 PM

View Postlar3ry, on 09 December 2012 - 04:43 PM, said:

View PostHeBTeMy, on 09 December 2012 - 04:36 PM, said:

I couldn't find Directory.EnumerateFiles(path,pattern) ...
i have a listbox that shows the names of the .txt files that it has in that directory, but i want when i select in listbox the text inside that selected file shows up on a richtextbox, and i can't figure how i get the current selecteditem's name...

i have this:
  Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim asd As String = ListBox1.SelectedItem
        RichTextBox1.LoadFile(Application.StartupPath & asd, RichTextBoxStreamType.PlainText)
    End Sub



and i don't understand why it doesn't load the txt when selected...

Place a breakpoint on the line RichTtextBox.Load ... and run your program. When it stops, check the conents of Application.StartupPath, and you'll find that it has no trailing "\", so you'll need to add it between the path and the filename.

Quote

The only way i can load something with that code is if i put
RichTextBox1.LoadFile(Application.ExecutablePath, RichTextBoxStreamType.PlainText)
   

and it loads me the .exe of the app itself...

The reason for that is that Application.ExecutablePath includes the filename of the executable.

EnumerateFiles() is part of the Directory Class, but if you already have the filenames in the ListBox, you won't need it. I'd be interested to know how you got them in there.

Documentation for it is on MSDN


Yeah, i forget it...

i got the names this way:
 Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim strPath As String = System.IO.Path.GetDirectoryName(Application.ExecutablePath) & "\"
        Dim dirInfo As New IO.DirectoryInfo(strPath)
        For Each file As FileInfo In dirInfo.GetFiles("*.txt", SearchOption.TopDirectoryOnly)
            ListBox1.Items.Add(file.Name)
        Next
    End Sub



Thanks for you help, your great man !
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1