I am trying to create an app which has a predefined set of data(currently a text file) and it reads from it, now how should i implement the data storage such that when i share the app with someone else, i don't have to pass the text file or any other data file(if possible, i can encrypt the contents and give it using a different extension) externally. I just want to give one exe file to the person and the data should be included inside the exe.
anyway to do it ??
Thanks in advance
Include the text file as a resource.
You do not need to change the build (if you are using Visual Studio that is). Visual Studio will embed the resource/file and generate a readonly property for the file so you can access it directly from within your code:
string fileContent = YourResourceFile.TheEmbeddedFile;
You could split the fileConent per linebreak but the previous line will load the entire file into memory.
string[] lines = fileContent.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
If the file is too big to be read into memory at once, you could stream the resource as explained here.
You can change the build mode of the file to resource/embedded resource, reading from it works different then, this should be answered somewhere though.
Related
I am working on an UWP text editor. I have added desktop extension to it to modify system files and other read only files. The problem I have is there is no reliable way to detect if a file has read-only attribute. FileInfo.IsReadOnly doesn't work and StorageFile.Attributes has FileAttributes.ReadOnly when file is dragged and dropped from file explorer.
How do I reliably check whether the file has read only flag or not?
While there is no way to detect the readonly attribute by using dotnet methods, however GetFileAttributesExFromApp can be used to get a lot of attributes(readonly, hidden etc.) of the file that aren't available via StorageFile api. Also, SetFileAttributesFromApp can be used to change/remove these attributes.
Edit
After some research and deep dive in MSDN, I came to know about RetrievePropertiesAsync(IEnumerable<String>) and
SavePropertiesAsync(IEnumerable<KeyValuePair<String,Object>>) methods for Windows.Storage.FileProperties.StorageItemContentProperties which can be used to get and set properties by name (Full list of properties names), the name System.FileAttributes can be used to get file attributes and can be used to detect if read-only flag is present. While retrieving properties always works modifying properties will only work if app has write access to file (Windows.Storage.StorageFile.Attributes doesn't contain ReadOnly flag), although SetFileAttributesFromApp works for that scenario but limitation of SetFileAttributesFromApp is it won't work for sensitive file types (.bat, .cmd etc.). So both those methods could be used combined to have maximum effect.
You can see the Attributes property has ReadOnly or not.
var filePicker = new Windows.Storage.Pickers.FileOpenPicker();
filePicker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
filePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.VideosLibrary;
foreach (string format in HelperUP.subtitlesFormat)
filePicker.FileTypeFilter.Add(format);
var file = await filePicker.PickSingleFileAsync();
if (file == null)
return;
Debug.WriteLine(file.Attributes);
The reason FileAttributes.ReadOnly throws an exception is that the System.IO APIs don't have access to arbitrary file locations on the hard drive in UWP.
On the other hand, a StorageFile opened in the app via drag & drop has this attribute set too, which is a problem that is continuously being discussed and hopefully will be fixed in a future version.
The only workaround I can think of (apart from always using the desktop extension) is declaring the broadFileSystemAccess capability (I have described the process here for example). This is a capability which gives you access to the whole filesystem and allows you to get a file using an arbitrary path with the StorageFile.GetFileFromPathAsync method (see Docs). Please note you will need to explain why this capability is required when you submit the application to the Microsoft Store.
With broad filesystem access, you could take the drag & drop StorageFile, take its Path and retrieve the same file again using StorageFile.GetFileFromPathAsync. This new copy of the file will no longer have the "false-positive" Read Only attribute and will reflect the actual attribute state from the filesystem.
Hello I'm coding a simple CRUDE application that runs perfectly using JSF and DB4O.
I can add and list all the entities normally without errors.
I've used this code to persist the entities
bd = Db4oEmbedded.openFile(configuration, "db.data");
bd.store(client);
bd.commit();
the problem is that I cant locate the db.data file. I,ve done a search in the whole PC and still cant find it.
soo where DB4O store those entities, how is it possible??
thank you.
It must be in your project execution path. Usually db4o file name is called with extension yap. Better you can use relative path to the database file like
String dbPath = "c:/db/project.yap"; // better read from project resource
EmbeddedObjectContainer openFile = Db4oEmbedded.openFile(dbPath);
I am trying to make a new file format for music. It needs to be a file that actually stores multiple audio files, for example a zip file. I am looking for a way to turn the zip file into this new file format. However, I still want to use id3 tags with these new files. I was wondering how I can make this new file format which is one file that holds multiple audio files, but still contains overall id3 tags for that one file, so that I can load it into my mobile applications.
Any help/recommendations would be appreciated.
Cheers,
AJ
The problem with creating your own new file format is that only you can use it. Until you convince lots of other people that it is a useful new format, no one else will have the tools to be able to do anything with the files you create.
For existing music player programs to be able to handle a new file format, you must write a CODEC for your file format in that player's plug-in style. Probably more than one plug-in as your file format is both a container of music and a catalog as well.
One alternative to creating a new file format is to put the MP3 files you have into a new MP3 file with each old file a new TRACK in the new file. Be sure to set each new tracks start time to be after the sum of all previous tracks play duration, so they don't step on each other. Merge the metadata about each file into the metadata of the new file. This might work OK for collections with lots of common metadata (like same artist), but might not work very well if the metadata is extremely varied.
Another alternative is to convert them to CDA format and put then into an Audio-CD image file, just as if you had burned them to a CD.
A third alternative is to put your files into an SQLite database file. Your metadata from each MP3 file fill in records, and you have your choice of leaving the MP3 file external and just linking to it, or storing the blob of your MP3 file in the DB as well. If you do store the blobs, then the SQLite database file is a single file that contains everything you put in it.
-Jesse
Don't create new formats unless you really really have a very good reason to do so.
Its sounds like Matroska can do anything you need. But in contrast to your own format you and other developers will have a bunch of ready to use tools to work with the format. This includes editors, players,... Additionally it you can leave making initial engineering errors to other people.
If you really really want to create your own format: Either just put your audio files that support id3 tags into your zip file, or create a meta file, for example in XML format, into your zip files as well, that contains the meta information that you want to be included.
I'm including a number of images as "Content" in my deployed XAP for Mango.
I'd like to enumerate these at runtime - is there any way to do this?
I've tried enumerating resources like:
foreach (string key in Application.Current.Resources.Keys)
{
Debug.WriteLine("Resource:" + key);
}
But the images aren't included in the list. I've also tried using embedded resources instead - but that didn't help. I can read the streams using Application.GetResourceStream(uri) but obviously I need to know the names in order to do this.
This is no API baked in to WP7 that allows you to enumerate the contents of the Xap. You need to know the name of the content items before you can retreive them.
There probably is some code floating around somewhere that is able to sniff out the Zip catalog in the XAP however I would strongly recommend that you don't bother. Instead include some sensible resource such as an Xml file or ResourceDictionary that lists them.
Having found no practical way to read the Content files from a XAP I build such a list at design time using T4.
See an example at https://github.com/mrlacey/phonegap-wp7/blob/master/WP7Gap/WP7Gap/MainPage.xaml.cs
This seems the right way to go as:
a) I'd rather build the list once at design time rather than on every phone which needs the code.
and
b) I shouldn't ever be building the XAP without being certain about what files I'm including anyway.
Plus it's a manual step to set the build action on all such files so adding a manual step to "Run Custom Tool" once for each build isn't an issue for me.
There is no way to enumerate the files set as "Content".
However, there is a way to enumerate files at runtime, if you set your files as "Embedded Resource".
Here is how you can do this:
Set the Build Action of your images as "Embedded Resource".
Use Assembly.GetCallingAssembly().GetManifestResourceNames() to
enumerate the resources names
Use
Assembly.GetCallingAssembly().GetManifestResourceStream(resName)
to get the file streams.
Here is the code:
public void Test()
{
foreach (String resName in GetResourcesNames())
{
Stream s = GetStreamFromEmbeddedResource(resName);
}
}
string[] GetResourcesNames()
{
return Assembly.GetCallingAssembly().GetManifestResourceNames();
}
Stream GetStreamFromEmbeddedResource(string resName)
{
return Assembly.GetCallingAssembly().GetManifestResourceStream(resName);
}
EDIT : As quetzalcoatl noted, the drawback of this solution is that images are embedded in the DLL, so if you a high volume of images, the app load time might take a hit.
How does one work with string resources in python/gae in eclipse+pydev environment? looking for something that is the equivalent of this link. Also, what is the recommended approach for working with large strings? Create a text file as part of your app and use file I/O, or something else?
for those still seeking a solution for string resources file on GAE and Python (2.7), I used a YAML formated file:
create a file named "strings.txt" (for example), it's content can be something like this:
level_1_name: this is a text
# this is a comment
test:
- testtitle: test title
testtitlechild: testtitlechild
place this on the bottom of your main application file:
# strings file (YAML format) initialization
strings = yaml.load(open("strings.txt", "r"))
then you get 'strings' as dict, using it goes as follows:
strings["level_1_name"]
Assuming "string resource" is a fancy name for text-file, you have three choices;
If your large text file is read only then you can bundle it along with your other application files and access it as you would normally (via open() or similar method).
If your application needs to write lots of text-data then you will have to store it in the datastore using a TextProperty but beware there are limits on the amoount of data that can be written to the datastore in one go (currently 1MB) ... OR...
Use the Blobstore API to read/write files