locked
How to create a file from URI?

    Question

  • I have a file path as URI like ms-appdata:\\\local\image.jpg.

    How can I create a file from this URI assuming the file does not exist already? I checks the API list available, but it seems the file can only be created from StorageFolder class. This is quite restrictive.

    That means, we should first transform the URI to local path, then parse the file name, open the folder, and then create the file? So much work for such a simple task? I am sure there must be an easy way to do this. Please help

    Tuesday, September 18, 2012 8:45 AM

Answers

  • You have to use the method you said & create the file using the local file path using StorageFile, it may be a lot of work to you but URI statements in windows 8 is to always assume that the location already exists, there is no URI methods you can use that will act differently to create any such file automatically(to ensure the URI points to something that actually exists)  There is no way to directly pass such a URI to any other method to create the file at that URI automatically for you because most URI methods at this point assume a networked or internet site is what your linking to using URI's & there-for will not try & create a file for you, because of possible permission problems, files have some URI functions of their own but will also only work with a local path string, which means you have to figure out the folder path & filename regardless, I know there was some function to pass in a full local path & it could parse out the folder from the file name & the extension, etc...but I would have to look for that function since I don't know it off the top of my head, I guess that is the part you don't want to handle is after using GetFileFromApplicationUriAsync() You don't want to have to create a method to split away the filename from the directory, and create the file, then finally able to use the original URI you started with, but it has to be done.
    Monday, September 24, 2012 2:39 PM

All replies

  • i think that's related with the problem discussed here ..

    http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/8529c9d8-27e6-4a73-b3b0-f09422635048

    no solution though..

    Tuesday, September 18, 2012 8:53 AM
  • Ya, I know there is no solution to convert from URI to local path. But, I am looking for something like GetFileFromApplicationUriAsync which does not involve local paths.

    And even if, I have a local path, how can create it to get StorageFile^?

    It seems I will have to parse the local path to separate the filename and path and then use StorageFolder to open the path and then create file from this StorageFolder instance? Too much work, I believe.

    Tuesday, September 18, 2012 8:58 AM
  • Hi,

    I think you can try CreateFileQueryWithOptions to find the file in some folder.
    Please follow the sample codes in this document
    http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.search.queryoptions.aspx

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Proposed as answer by Jesse Jiang Monday, September 24, 2012 3:06 AM
    • Unproposed as answer by John Rick Monday, September 24, 2012 10:35 AM
    Wednesday, September 19, 2012 11:28 AM
  • I see no connection between your reply and my question. The question about creating a non-existent file easily with a given URI.
    Monday, September 24, 2012 10:37 AM
  • You have to use the method you said & create the file using the local file path using StorageFile, it may be a lot of work to you but URI statements in windows 8 is to always assume that the location already exists, there is no URI methods you can use that will act differently to create any such file automatically(to ensure the URI points to something that actually exists)  There is no way to directly pass such a URI to any other method to create the file at that URI automatically for you because most URI methods at this point assume a networked or internet site is what your linking to using URI's & there-for will not try & create a file for you, because of possible permission problems, files have some URI functions of their own but will also only work with a local path string, which means you have to figure out the folder path & filename regardless, I know there was some function to pass in a full local path & it could parse out the folder from the file name & the extension, etc...but I would have to look for that function since I don't know it off the top of my head, I guess that is the part you don't want to handle is after using GetFileFromApplicationUriAsync() You don't want to have to create a method to split away the filename from the directory, and create the file, then finally able to use the original URI you started with, but it has to be done.
    Monday, September 24, 2012 2:39 PM