|
-
May 22nd, 2012, 04:22 PM
#1
Thread Starter
Lively Member
What's the best location to write files to? (on the end-user's hard drive)
Hey guys,
So I'm working on an app that needs to create files on the end user's computer to store info in (mainly text files and HTML files), and I'm trying to figure out the best location to write these files to. I've been reading various articles @ this online all afternoon, and the advice I've read so far seems to be "all over the place" , so I thought I'd come here and get some feedback from you guys about this.
From what I understand, if the end user is not logged into Windows as "admin", then there are a limited number of locations where files can be written to their computer, without them having to deal with UAC popups, etc.
From what I've read so far, these locations are...
* App Data folder (My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData)
* Temp folder (System.IO.Path.GetTempPath)
Is that correct? And are there any other locations?
I would like my end-user's to have to jump through as few hoops as possible (none, preferably ), so should I only write my files to these specific locations?
FYI... when I write the files to the end-user's computer, I have 2 different types of info that needs to be stored...
Info "A" is temporary data that only needs to be stored/accessed while the end user has my program open (and this data can be safely deleted each time they close my program).
Info "B" needs to be stored somewhere where it never gets deleted, so that it can be accessed by my program for as long as the end-user has my program installed on their computer.
So would it be best to store info "A" in the Temp folder, and info "B" in the App Data folder?
And for any files that I store in the "temp" folder, do these files get automatically deleted when the end-user turns off their computer, or do I have to programatically delete these files every time my program exits?
Any advice on this would be greatly appreciated!
-
May 22nd, 2012, 04:34 PM
#2
Hyperactive Member
Re: What's the best location to write files to? (on the end-user's hard drive)
Will the end user ever need to access these files manually? If not, the %AppData% directory is probably your best option if the files need to persist, as in your Info "B" (If this is program settings information, though, and not application data, you should consider storing it in the registry.)
Whether the temp files get deleted automatically can depend on a few things. Under default settings, they probably won't be deleted automatically, but I certainly wouldn't store anything in the %Temp% directory that needs to be used again later. But if you only need to use them while the program is running, such as in your Info "A" example, %Temp% should be fine. (If you want the files to be deleted when the program closes, I would suggest creating a directory within %Temp% and then deleting the entire directory on application exit.
-
May 22nd, 2012, 11:18 PM
#3
Hyperactive Member
Re: What's the best location to write files to? (on the end-user's hard drive)
IMHO I think that My Documents are always there for exactly this reason - every user has its own My Documents folder and this folder is always read/write enabled for this User (guest,standard,admin).
So for writing some data to disk on different user machines I would definitely recommend doing it in My Documents/SubFolder folder.
For App Settings Use My.Settings class that is automatically written in per user read/write enabled folder under Documents&settings.../userName/...
For temporary data use Temp folder as already suggested. Temp folder is shared between Users. After using it, clean it.
-
May 22nd, 2012, 11:39 PM
#4
Re: What's the best location to write files to? (on the end-user's hard drive)
I haven't read the other replies in detail but here's my summary:
1. If it's a file that you want the user to be able to access independent of your application, definitely the Documents folder. There is a per-user Documents folder and also a common Documents folder, so choose whichever is most appropriate for the content. Both are read/write for all users.
2. If data is for the application's use rather than the user's, consider the Application Data folder. There is a per-user folder that is read/write for all users and a common folder that is read/write for administrators only. If you want to save common application data for limited users then use the common Documents folder.
3. If data will exist for a short time during the current session only then use the Temp folder.
All these and more special folders are accessible via Environment.GetFolderPath and My.Computer.FileSystem.SpecialDirectories.
-
May 23rd, 2012, 08:19 AM
#5
Hyperactive Member
Re: What's the best location to write files to? (on the end-user's hard drive)
Am I the only one who gets pissed off when an app puts folders/directories in my My Documents folder? I've uninstalled several useful programs because of this. I keep my My Documents folder structured in a very specific manner and don't want it mussed up with random folders popping in.
If the documents aren't something the user will need to interact with on the file level (by which I mean double clicking on a particular document to open it or browsing through explorer for particular files) then keep the files out of sight.
-
May 23rd, 2012, 09:42 AM
#6
Thread Starter
Lively Member
Re: What's the best location to write files to? (on the end-user's hard drive)
Thanks for the feedback, guys.
-
May 23rd, 2012, 01:31 PM
#7
Hyperactive Member
Re: What's the best location to write files to? (on the end-user's hard drive)
 Originally Posted by samuelk
Am I the only one who gets pissed off when an app puts folders/directories in my My Documents folder?
No you are not
-
May 23rd, 2012, 03:06 PM
#8
Re: What's the best location to write files to? (on the end-user's hard drive)
The following shows how to create a temporary file that will be removed when closing the app and should get removed (but not guaranteed) if killed in Task Manager.
The FileStream will only be accessible to your app, outside of the app it cannot be read.
Code:
''' <summary>
''' This code demonstrates how to create a temporary file, use it during
''' the course of the application lifetime then remove the file upon
''' closing the file stream when the application is closed.
''' </summary>
''' <remarks></remarks>
Public Class Form1
Private CustomersDocument As XDocument
Private TempFileName As String = IO.Path.Combine(Application.StartupPath, "TempCustomers.xml")
Private TheCreator As New System.IO.FileStream(
TempFileName,
System.IO.FileMode.Create,
System.Security.AccessControl.FileSystemRights.Modify,
System.IO.FileShare.None, 8, System.IO.FileOptions.DeleteOnClose
)
Private MyCustomersReader As System.IO.StreamReader
Private Sub LoadCustomerData()
CustomersDocument = XDocument.Load("Customers.xml")
DataGridView1.DataSource = (
From customer In CustomersDocument...<Customer>
Select Identifier = customer.<CustomerID>.Value,
ContactName = customer.<ContactName>.Value,
CompanyName = customer.<CompanyName>.Value,
Country = customer.<Country>.Value).ToList
DataGridView1.Columns("Identifier").Visible = False
DataGridView1.AutoResizeColumns()
ActiveControl = DataGridView1
Dim Mexico = ( _
From customer In CustomersDocument...<Customer>
Where customer.<Country>.Value = "Mexico"
Select Identifier = customer.<CustomerID>.Value,
ContactName = customer.<ContactName>.Value,
CompanyName = customer.<CompanyName>.Value,
Country = customer.<Country>.Value).ToList
Dim Temp = <Customers>
<%= From customer In CustomersDocument...<Customer>
Where customer.<Country>.Value = "Mexico"
Select <customer><CompanyName><%= customer.<CompanyName>.Value %></CompanyName></customer> %>
</Customers>
Dim MexicoCustomersByteArray As Byte() = System.Text.Encoding.ASCII.GetBytes(Temp.ToString)
TheCreator.Write(MexicoCustomersByteArray, 0, MexicoCustomersByteArray.Length)
End Sub
''' <summary>
''' 1. Mark temporary file as a temporary file.
''' 2. Load data into DataGridView, save Mexico customers to temp file.
''' 3. Read data in temporary file into an XDocument
''' 4. Read results using LINQ into a ListBox
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub Form1_Load(
ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
System.IO.File.SetAttributes(
TheCreator.Name,
System.IO.File.GetAttributes(TheCreator.Name) Or System.IO.FileAttributes.Temporary
)
LoadCustomerData()
MyCustomersReader = New System.IO.StreamReader(TheCreator)
MyCustomersReader.BaseStream.Seek(0, IO.SeekOrigin.Begin)
Dim MexicoData As String = ""
While (MyCustomersReader.Peek > -1)
MexicoData &= MyCustomersReader.ReadLine
End While
ListBox1.DataSource = (From customer In XDocument.Parse(MexicoData)...<customer>
Select customer.<CompanyName>.Value).ToList
End Sub
''' <summary>
''' Close file streams which removes them from disk.
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub Button1_Click(
ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click
MyCustomersReader.Close()
TheCreator.Close()
Close()
End Sub
End Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|