I have a picture file that I want to copy to a folder within the app every time the app is opened in case the user accidentally deletes it. I want the picture file to be stored within the app itself, so I drag the picture file directly into the project. The name of this file is “default.jpg”. My question is, how do I copy default.jpg from the project to a folder?
The code I have below can copy a file from the root directory to the folder, but I rather have the default.jpg embedded in the application rather than adding the file to the root directory. Does anyone have any ideas how I can do this?
[code]Dim ft as new filetype
Dim f As FolderItem
f = GetFolderItem(“default.jpg”)
ft.name=txtFile.Text
//Copies file to target location
f.CopyFileTo(getfolderitem(“Inventory”).child(ft.name))[/code]
If you want it to be within the app, just drag it into the project at design time.
Some one would really need to be hacking to delete it from the app contents later.
In code, refer to it as default instead of using a folder item and an openaspicture statement
The project item will be named “default”, and you will need to use Picture.Save to write it to disk. Something like
dim f as folderitem
f = getfolderitem("Inventory").child("default.jpg")
default.save(f)
Refer to the docs for the various parameters to Picture.Save. And as was mentioned in your previous thread, you will need to turn on GDI+ to save a jpg on Windows.
I could not get that to work. I guess I will just store the image in the root folder and use the code below to copy it to the Inventory folder. That works. I would rather have it copy from an embedded image stored inside the app, but this will due. Thanks for your help.
[code] //Load Default images so no one deletes them
Dim ft as new filetype
Dim f As FolderItem
f = GetFolderItem(“default.jpg”)
ft.name=txtFile.Text
//Copies file to target location
f.CopyFileTo(getfolderitem(“Inventory”).child(ft.name))
f.CopyFileTo(getfolderitem(“Pictures”).child(ft.name))[/code]
If you do this on a windows machine, it will fail.
You cannot (normally) write to a folder in the program files folder.
Your files belong in Specialfolder.Applicationdata.child(“Myapp”)
You should create that folder when your app runs.
JPG needs special libraries and the like. I prefer PNG myself.
Try this code:
Start a desktop project.
Drag default.png into the project
Add this code to the Open event of the app
[code] dim myfolder as folderitem
dim myfile as folderitem
myfolder = specialfolder.ApplicationData.child(“myapp”)
if myfolder.exists = false then
myfolder.CreateAsFolder
end if
myfile = myfolder.child(“default.png”)
if my file.exists = false then
default.Save myfile,150
end if
[/code]
If you make myfile a property of the app, then you can access it anywhere in your code as app.myfile
But since you already have default in memory, why write it to disc unless you want the user to be able to amend it?