Win8.1 Application Development File operations

Source: Internet
Author: User

Before manipulating the file, set the app function declaration for the application. Users can access files on win8.1 through C # (non-UI), only four folders of pictures, music, videos and documents. The file picker allows access to 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 you can manipulate the files and folders 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 scope is typically used in an automated jukebox app that requires access to the entire music library.
File Picker provides a powerful UI mechanism that allows users to open files to be processed by an app. The Musiclibrary license scope should be declared only if the application requires programmatic access and the use of file picker is not possible for programmatic access.


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 scope is typically used in photo playback applications that require access to the entire picture library.
File Picker provides a powerful UI mechanism that allows 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 the use of file picker is not possible for programmatic access.


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 scope is typically used in movie playback applications that require access to the entire video library.
File Picker provides a powerful UI mechanism that allows users to open files to be processed by an app. The Videoslibrary license scope should be declared only if the application requires programmatic access and the use of file picker is not possible for programmatic access.


For access to the Documents folder, set it automatically based on the error information provided by VS, then set the file association and follow the prompts.


(ii) file picker

In the form of UI, access to files on the entire system. Use the file picker to access files and folders by letting users select files and folders. You can use the Fileopenpicker class to get access to the file and use Folderpicker to get access to the folder. With the file picker, your app can gain access to files and folders on the user's entire system. When you invoke the file picker, users can browse their systems and select files (or folders) to access and save. After the user picks up a file or folder, your app receives those selections as StorageFile and Storagefolder objects. Your app can then manipulate the selected files and folders 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

The KnownFolders class provides access to common locations where user content is contained. This includes content from users ' local libraries (such as photos, documents, music, or videos), removable devices, homegroup, and media server devices. For accessing disk files, this is limited to four folders of pictures, music, documents and videos.

Example 1: Download a file from the server to a sub-file (dynamically created) under the Pictures folder

 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: Connect to Example 1 to read the downloaded file

  Gets the specified file under the specified directory            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);            }


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.