We help IT Professionals succeed at work.

Creating and saving a Logfile on a deployed VB.NET 2010 express application

slightlyoff
slightlyoff asked
on
I have code that appends a logfile every time the program runs.  The program is on a timer, so it restarts every 10 minutes and writes to a text file.

Dim FILENAME As String = "logfile.txt"
Dim objStreamWriter As StreamWriter

objStreamWriter.WriteLine("Text Line 1")
objStreamWriter.WriteLine("Text Line 2")
objStreamWriter.Close()

In debug mode, the logfile.txt is saved in the bin/debug folder
When i publish the application, I'm not sure where the logfile.txt file gets saved.

The file path is C:\Users\MyName\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Microsoft after I've installed the application, but the logfile isn't there.

Where would it be saved?  I search the computer but it doesnt' turn up.

I hope this question makes sense.

Thanks!

Comment
Watch Question

Most Valuable Expert 2012
Top Expert 2014

Commented:
It would be saved in the same folder as the executable. Start menu would contain the shortcut only.

Commented:
try :
Dim FILENAME As String = Application.StartupPath() & "\logfile.txt"

that will probably save it in the same folder as the application
Top Expert 2015
Commented:
Both previous answers are good, but you have to be aware that many directory typically used to install a file are readonly for most users that are not administrator from a .NET application.

It is usually better to provide a path to the user application path for files that need to be written to. The most standard path to use for that can be defined the following way:

IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "<Folder for your company>", "<Folder for your application>") & "\<Your File>"

The final path will be dependant on the operating system. You seem to be on Vista or Windows 7, so it should go down under C:\Users\Jacques\AppData\Roaming. ApplicationData is the recommended place to store application related information that need to be written to from time to time.

Author

Commented:
I think part of the problem is how I am publishing.  I don't choose where the file gets installed.  i use the Build > Publish My Program option in Visual Basic 2010 Express.  

It doesn't give me many options as far as where it's ultimatly installed.

When you say:
IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "<Folder for your company>", "<Folder for your application>") & "\<Your File>"

where do the values for :"<Folder for your company>", "<Folder for your application>" & <Your File> get specified?

Thanks for your help, sorry if that's a dumb question.
Top Expert 2015

Commented:
You cannot specify where to install the program when working with ClickOnce. If you need to install in a specific location, you have to create a standard Setup deployment, but you lose the automatic update feature that makes ClickOnce so interesting to use.

where do the values for :"<Folder for your company>", "<Folder for your application>" & <Your File> get specified

Right in the code, something such as the following, although I usually use constants defined somewhere in the application or a dll instead of straight Strings:

IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "JBFI", "JBWoodwork") & "TypeOfWoods.xml"

The name of the application can also be retrieved the following way:

IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "JBFI", Application.ProductName) & "TypeOfWoods.xml"


Author

Commented:
Thank you!

Author

Commented:
http://www.daniweb.com/software-development/vbnet/threads/249133

I looked up IO.Path.Combine - and found the above article.  I'm all set. Thanks for the help!