File access sample

This sample shows how to create, read, write, copy and delete a file, how to retrieve file properties, and how to track a file or folder so that your app can access it again. This sample uses Windows.Storage and Windows.Storage.AccessCache API.

 
 
 
 
 
(33)
107,228 times
Add To Favorites
4/9/2013
E-mail Twitter del.icio.us Digg Facebook
Sign in to Ask a Question


  • Javascript doesn#t work on Win7 with IE9
    1 Posts | Last Post June 07, 2013
    • Hi,
      I tried to use the javascript but it doesn't work.
      I use IE9 on Win7.
      BR
      Markus
  • Good sample code
    3 Posts | Last Post March 15, 2013
  • The Write a File doesn't show how to overwrite a file
    3 Posts | Last Post February 27, 2013
    • If a file exists, writing to it will only replace the first n bytes that are being written, leaving the rest behind. How can I overwrite a file?
    • Hey David, you can truncate the content of the file by assigning a value to the size property of the acquired read/write stream. In this case if you want to reset only what is left behind you can assign the number of bytes n that you just wrote to the size. You can also start from a clean file if you go assign zero to the size of the stream before you write to it.
    • // Overwite use CreationCollisionOption.ReplaceExisting
         StorageFile file = await crmImgsFolder.CreateFileAsync(TEMPFOLDER_IMAGEPREFIX + id.ToString(), CreationCollisionOption.ReplaceExisting);
  • Exception of "KnownFolders.DocumentsLibrary"
    1 Posts | Last Post February 05, 2013
    • 1. I can't open the solution using Visual studio Express 2012 for Windows phone in windows phone 8 RTW(SDK 8.0)
      2. When i try to use the "StorageFolder storageFolder = KnownFolders.DocumentsLibrary;"
      in my project, it raises the "NotImplementException". (SDK 8.0)
      
      Can anybody help to answer my question?
      Thanks.
  • Accessing Files in Background task..
    1 Posts | Last Post December 20, 2012
    • I want to read files from system and update the tiles... by using background task.....here is my code of backgroundtask... my code is in c++.
      But i am not able to read the file.. it is working in MainPage.xaml.cpp but not in BackgroundTask... Class1.cpp
      
      void Class1::Run(IBackgroundTaskInstance^ taskInstance)
      {
      	BackgroundTaskDeferral^ deferral = taskInstance->GetDeferral();
      	TileUpdateManager::CreateTileUpdaterForApplication()->Clear();
      	TileUpdateManager::CreateTileUpdaterForApplication()->EnableNotificationQueue(true);
      	Notifications();
      	deferral->Complete();
      
      }
      
      void Class1::Notifications()
      {
      create_task(KnownFolders::DocumentsLibrary->GetFileAsync("12.txt")).then([this](task<StorageFile^> getFileTask)
      {
      	try
          {  auto _sampleFile = getFileTask.get();
      		if (_sampleFile != nullptr){
      
              create_task(FileIO::ReadTextAsync(_sampleFile)).then([this](String^ fileContent)
      
              {
      		Sting^ readFile = String^ fileContent;
      
           
      
              });
      
      		}
      
      	}
      	catch(Exception^ ex)
      	{
      	}
      });
      }
  • Accessing Files in Background task..
    1 Posts | Last Post December 18, 2012
    • I need to read some files and update the tiles automatically which are in my system.. by using background task but.. i am not able to do it.. can any one help me 
  • Accessing files from other than KnownFolders
    5 Posts | Last Post December 08, 2012
    • What has changed around accessing folders that do not exist in the KnownFolder class?  For instance, say the OEM wants to parse all of the OEM#.INF files from the "c:\\Windows\\Inf" directory using code?  What is the CORRECT WINDOWS 8 way to authenticate the process and gain programatic access to the folder in order to return the OEM#.inf files?
    • Hi Christian,
      
      With regards to your question, this depends on the type of application that the OEM is trying to develop. If this is a Desktop application you should still be able to have access to the folder just as in the past. However if this is a Metro style application then it will only have acess to locations defined by its capabilities:
      * How to specify capabilities in a package manifest: http://msdn.microsoft.com/en-us/library/windows/apps/br211477(v=vs.85).aspx
      * Access to user resources using the Windows Runtime: http://msdn.microsoft.com/en-us/library/windows/apps/hh464936(v=vs.85).aspx
      
      Therefore, the Windows folder will not be programatically accessible in Metro Style applications unless user selected through the File Picker (http://code.msdn.microsoft.com/windowsapps/File-picker-sample-9f294cba)
    • After re-reading my response I noticed that my last statement was partially incorrect. For Metro Style applications, those directories which are not part of the user resources or supported locations can be accessible through the File Picker. However, the Windows directory is special such that it is not accessible through Metro Style applications. Have you considered using AppData to store those OEM#.inf files instead?
    • Thank you, Elmar!!  This is what I thought was the case!  I just created a desktop app and got to the info that way...  It is pretty clear that tool development will be fairly limited to desktop apps.
    • This was very interesting. Thanks for the sample and the comments.
  • How to save file in C or D drive ?
    2 Posts | Last Post November 13, 2012
    • I don't want to save files in default library folder. I want to save file at any location. Is there any way ? Thanks.
    • There should be a dropdown in the upper left-side of the Open/Save dialog screen with the name of default library folder. If you click on it, it should give you more options where to open/save. Hope this helps.
  • why don't any of these code samples work?
    3 Posts | Last Post September 24, 2012
    • In this case getting 69 missing declaration errors when I download and compile the VB version of this.  Missing some included files???
    • Hey cribflipper, I am unable to reproduce this myself so I would like to get some more information on this. Could you provide the Windows 8 and VS 2010 version that you are using so that I can follow up? 
    • cribflipper, 
      Every thing is running smoothly!
  • CSV FILES
    8 Posts | Last Post September 24, 2012
    • Hello can any one tell me please how can i read a csv file from the temporary folder ?
    • Could you please elaborate a bit more on your question? So far I see a two fold question here and would like to get a bit more of clarification on what you are looking to accomplish before an answer is provided. When you mention temporary folder, I assume you are referring to the application data temporary folder. Is this correct? Also, when reading a CSV file is your question related to how to read a file in general or how to accomplish the parsing of a CSV file? Finally, what is your target programming language?
    • there is an example how to open the local folder/files here ( http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh700361.aspx ). And reading a local storage file is something like:
      
            StorageFile sampleFile = await localFolder.GetFileAsync("dataFile.txt");
            String timestamp = await FileIO.ReadTextAsync(sampleFile);
      
      As for the CVS, you might need to parse the content yourself. I am not aware of a mechanism that you can just load the data and read from a table.
    • Can has the right approach for reading from a file. There are several methods in which you can read from file either using the FileIO (http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.fileio.aspx) or PathIO (http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.pathio.aspx) helpers if memory is not a problem (e.g. the file size is small enough to fit in-memory with a single read) or you can acquire a stream (http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.storagefile.openasync.aspx) to the file and parse it (using DataReader, http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.streams.datareader.aspx) in chunks (if the file is large enough). You can write your own CSV parser like below. A bit of warning here, while I tried to make sure that the below works correctly there might be some further testing and error handling needed, so take this as a proof of concept.
      
    • Here is the proof of concept of how you would go about parsing in JavaScrpt and the existing API's:
      function parseCsvLine(line) {
          var columns = [];
          var column = "";
          for (var charNumber = 0; charNumber < line.length; charNumber++) {
             if ((line[charNumber] === ",") || (charNumber === line.length - 1)) {
                columns.push(column + ((line[charNumber] !== ",") ? line[charNumber] : ""));
                column = "";
             } else {
                column += line[charNumber];
             }
          }
      
          if (line[line.length - 1] === ",") {
             columns.push("");
          }
      
          return columns;
      }
      
      function parseCsvFileLines(lines) {
         var myCsvFile = [];
         for (var lineNumber = 0; lineNumber < lines.length; lineNumber++) {
             var columns = parseCsvLine(lines[lineNumber]);
             myCsvFile.push(columns);
         }
      
         return myCsvFile;
      }
      
      Windows.Storage.ApplicationData.current.temporaryFolder.getFileAsync("data.csv").then(function (file) {
         return Windows.Storage.FileIO.readLinesAsync(file);
      }).then(function (lines) {
         return parseCsvFileLines(lines);
      }).then(function (myCsvFile) {
         for (var lineNumber = 0; lineNumber < myCsvFile.length; lineNumber++) {
            for (var columnNumber = 0; columnNumber < myCsvFile[lineNumber].length; columnNumber++) {
               printConsoleLine("Line " + lineNumber + ", Column: " + columnNumber + ": " + myCsvFile[lineNumber][columnNumber]);
            }
         }   
      });
    • @elmar: hehe, nice to see javascript is catching up :)
    • upps wrong profile.. :) continuing the javascript discussion... how is it comparing to c# or vb... still haven't gotten the chance to work with that type of metro app...
    • Soufinx : follow this -> http://www.daveoncsharp.com/2009/09/how-to-use-temporary-files-in-csharp/
1 - 10 of 11 Items