Win8.1 Application Development File operations

Source: Internet
Author: User

Before manipulating the file, the corresponding application function declaration is set. Users who access files on win8.1 through C # (non-UI) can only be limited to four catalogs of pictures, music, videos and documents.

Through the file picker, you can access the entire system's files.

(i) Application function statement

For WIN8 store apps, open the Package.appxmanifest file. Click on the "Features" tab, tick "Music Library", "Picture Library" and "video library" so that you can manipulate the files and directories in the code:


Music
The Musiclibrary licensing scope provides programmatic access to user music, allowing applications to enumerate and access all files in the library without user interaction . This license range is often used in your own active Jukebox app that requires access to the entire music library.
File Picker provides a powerful UI mechanism to enable users to open files to be processed by an app.

Only when the application requires programmatic access, and the use of file picker is not possible to implement programmatic interviewing. The scope of the Musiclibrary license should be declared.


Image
The Pictureslibrary licensing scope provides programmatic access to user images, allowing applications to enumerate and access all files in the library without user interaction.

This license range is often used in photo playback applications that require access to the entire picture library.


File Picker provides a powerful UI mechanism to enable users to open files to be processed by an app. The Pictureslibrary license scope should be declared only if the application requires programmatic access and cannot be programmed by using file picker.


Video
The Videoslibrary licensing scope provides programmatic access to user video, allowing applications to enumerate and access all files in the library without user interaction. This license range is often used in movie playback applications that require access to the entire video library.


File Picker provides a powerful UI mechanism. Enables users to open files to be processed by an app. Only when the application requires programmatic access, and the use of file picker is not possible to implement programmatic interviewing. The scope of the Videoslibrary license should be declared.


For access to the documents directory, make your own proactive settings based on the error information provided by VS, then set the file associations and follow the prompts.


(ii) file picker

UI form. Access to files on the entire system.

Use the file picker to access files and directories by letting users select files and directories. You can use the Fileopenpicker class to get access to a file, and use Folderpicker to get access to the directory.

With the file picker, your app can get access to files and directories on the user's entire system. When you call the file picker. Users can browse their systems and select files (or directories) to access and save.

After the user selects a file or directory, your app receives the selections as StorageFile and Storagefolder objects. Your app can then manipulate the selected files and directories by using these objects.

if (rootpage.ensureunsnapped ()) {    Fileopenpicker openpicker = new Fileopenpicker ();    Openpicker.viewmode = Pickerviewmode.thumbnail;    Openpicker.suggestedstartlocation = pickerlocationid.pictureslibrary;    OPENPICKER.FILETYPEFILTER.ADD (". jpg");    OPENPICKER.FILETYPEFILTER.ADD (". jpeg");    OPENPICKER.FILETYPEFILTER.ADD (". png");    StorageFile file = await openpicker.picksinglefileasync ();    if (file = null)    {        //application now have read/write access to the picked file        outputtextblock.text = "Picke D Photo: "+ file. Name;    }    else    {        Outputtextblock.text = "Operation cancelled.";    }}

(iii) file operation by programming

Class KnownFolders provides access to a common location that includes user content. This includes content from users ' local libraries (such as photos, documents, music, or videos), removable devices, homegroup, and media server devices. For access to disk files. This is only limited to four catalogs of pictures, music, documents and videos.

Demo Sample 1: Download a file on the server to a sub-file (dynamically created) in the pictures directory

 String Dync_ipv4 = "211.87.237.23";            String port = "8081";            String Urljsonpath = "Json/images.txt";            String Urljsonpath = "Image/694021692/1214936171.png";            String uri = "/http" + Dync_ipv4 + ":" + port + "/" + Urljsonpath;            System.Diagnostics.Debug.WriteLine (URI);            string filename = "ImagesUri.txt";            string filename = "Daxia.png";            var Rass = Randomaccessstreamreference.createfromuri (new Uri (URI)); Irandomaccessstream InputStream = await rass.            OpenReadAsync ();                      Stream input = windowsruntimestreamextensions.asstreamforread (Inputstream.getinputstreamat (0));                try {//Get the Picture extension GUID Storagefolder folder = Knownfolders.pictureslibrary; System.Diagnostics.Debug.WriteLine (folder.                Path); Creates a new file in the current folder, and specifies how to does if a file with the same name already existsIn the current folder. Storagefolder Childfolder = await folder.                Createfolderasync ("Wherewego");                StorageFile OutputFile = await childfolder.createfileasync (filename, creationcollisionoption.replaceexisting); StorageFile OutputFile = await folder.                Createfileasync (filename, creationcollisionoption.replaceexisting);                System.Diagnostics.Debug.WriteLine (Outputfile.path);                    using (Irandomaccessstream OutputStream = await Outputfile.openasync (fileaccessmode.readwrite)) {                    Stream output = windowsruntimestreamextensions.asstreamforwrite (Outputstream.getoutputstreamat (0)); await input.                    Copytoasync (output); Output.                    Dispose (); Input.                Dispose ();            }} catch (Exception) {System.Diagnostics.Debug.WriteLine ("adfasd");  }

Example 2: Sample 1 is shown. Read the downloaded file.

  Gets the specified file under the specified folder            storagefolder storagefolder = knownfolders.pictureslibrary;            Storagefolder folder = await Storagefolder.getfolderasync ("Wherewego");            StorageFile StorageFile = await folder. Getfileasync ("ImagesUri.txt");           StorageFile StorageFile = await Storagefolder.getfileasync ("ImagesUri.txt");            if (storageFile! = null)            {                //Gets the text content in the specified file                string textcontent = await Fileio.readtextasync (StorageFile, WINDOWS.STORAGE.STREAMS.UNICODEENCODING.UTF8);                System.Diagnostics.Debug.WriteLine (textcontent);            }


Win8.1 Application Development File operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.