This is something I know how to do easily in VB6…but struggling with Xojo.
I want my Program to open a comma delimited CSV file that I would distribute in the same folder as the executable. This CSV file just contains temperature data records. Because I know the location and name of the file - I don’t want the user to have to open it with a File Open dialog.
The following code runs when a menu item is selected. It compiles fine but when running in the IDE or compiled EXE throws an exception:
[code]var buff as string
var CurrentLine as String
Var t As TextInputStream
Var myFile As FolderItem
myfile.Name=“temperature.csv”
t = TextInputStream.Open(myFile)
While Not t.EndOfFile
currentline= t.ReadLine
buff=currentline.trimleft
'########################################################################################
'Put code to manipulate lines of CSV data here
straight from one of the xojo examples, I’ve added my own comments to help:
Dim file As FolderItem = f
Dim input As TextInputStream
If file Is Nil Then
' you didn't pass in an "f" so we need to ask the user. In your scenario,
' you know what the file is, so you are passing it in as the "f" to begine with
' set is as a property of your project
file = GetOpenFolderItem(XojoTextFileTypeSet.XojoText)
End If
If file <> Nil Then
' This is just confirming that (given there was an option to load it above)
' there was in fact a file loaded
input = TextInputStream.Open(file)
EditArea.Text = input.ReadAll
input.Close
End If
We can make it shorter for your case:
Set a property in your project of “myFile” that is of FolderItem type.
Then you load that in when you app starts (aka "open"s):
Dim input As TextInputStream
' You should still make sure the file is not nil
If myFile <> Nil Then
input = TextInputStream.Open(myFile)
EditArea.Text = input.ReadAll
input.Close
End If
Process your CSV as relevant.
I do believe there is a CSV example I’ve seen, just can’t find it right now.
var myFile as FolderItem = SpecialFolder.Documents.Child("temperature.csv")
Kevin, these throw up lots of errors:
myfile = NewFolderItem(temperature.csv)
Or
Var myfile = NewFolderItem(temperature.csv)
Dave, Sorry - but where does the filename “temperature.csv” get assigned? I tried setting it in the default property (error: expected class FolderItem but got String)
[quote=497943:@Mark Cresswell]Markus, this created an IOException:
var myFile as FolderItem = SpecialFolder.Documents.Child("temperature.csv")
Kevin, these throw up lots of errors:
myfile = NewFolderItem(temperature.csv)
Or
Var myfile = NewFolderItem(temperature.csv)
Dave, Sorry - but where does the filename “temperature.csv” get assigned? I tried setting it in the default property (error: expected class FolderItem but got String)[/quote]
That should be:
myfile = New FolderItem(temperature.csv) // note the space
Then you need to check that myfile is not Nil, that myfile.Exists is true, etc. Personally I tend to use the native path, so mine look like:
outputf = new FolderItem ("/path/to/my/myfile", FolderItem.PathModes.Native)
[quote=497943:@Mark Cresswell]Markus, this created an IOException:
var myFile as FolderItem = SpecialFolder.Documents.Child("temperature.csv")
Kevin, these throw up lots of errors:
myfile = NewFolderItem(temperature.csv)
Or
Var myfile = NewFolderItem(temperature.csv)
Dave, Sorry - but where does the filename “temperature.csv” get assigned? I tried setting it in the default property (error: expected class FolderItem but got String)[/quote]
Apologies, there should have been a space between new & FolderItem.
I recommend that you read up on the FolderItem docs.
[quote=497943:@Mark Cresswell]Markus, this created an IOException:
var myFile as FolderItem = SpecialFolder.Documents.Child("temperature.csv")
[/quote]
had you put this file in Documents folder before? if it not exists there it can not open.
i guess the IOException message said something useful.
you could add a BuildStep Copy Files into the Resources folder (as template file)
and if you need it copy it to ApplicationData once at runtime and there you can edit this data.
Note that if you ever create an app for Mac, opening files silently this way is not longer possible, apart in SpecialFoleder.ApplicationData, or in Specialfolder.Resources.
If, apparently “Temperature.csv” is a work file, placing it in the same folder as the executable is bad form. One of these days, Windows will not permit it.
Ideally, place that in SpecialFolder.ApplicationData.
Thanks All - I got it to work.
Having to put reference files somewhere other than the location of the executable seems less elegant to me. Operating systems seemt o be getting more restrictive.
Windows indeed is getting more restrictive. For the moment, “old style” apps that reside in Program Files (x86) can still modify files next to the executable, but the new UWP Application Model prohibits it.
UWP Apps, otherwise called “New API”, are typically more robust apps installed natively in Windows 10, such as the Edge browser, or the apps available in the Windows Store.
I don’t know if I would call elegant the possibility to damage executable files by accident. I find way more elegant the Mac that prevent writing on any file placed in /Applications (the equivalent of Program Files).
It is a good habit to place working files that you need to keep in a subfolder of SpecialSystem.ApplicationData.
That way, if you need to reinstall for any reason, you don’t destroy all the user data.