locked
Error when trying to get/create file RRS feed

  • Question

  • Hello,

    So I have some C# code inside a WinRT Component that tries to create an empty file if it does not exist. I have the following code to create a file:

    try
                    {
                        StorageFolder folder = storageLocation;
                       
                        file = await folder.GetFileAsync(dest);
    
                        // If file already exists, delete it
                        if (file != null)
                        {
                            var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
    
                            try
                            {
                                await file.DeleteAsync(StorageDeleteOption.PermanentDelete);
                                removed = true;
                            }
                            catch (Exception)
                            {
                                // If file existed and was not deleted, fail the download
                                status = Enumerations.DMODownloadStatus.DownloadFailed;
                                removed = false;
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        file = null;
                    }
    
                    try
                    {
                        if (!removed)
                        {
                            return false;
                        }
    
                        // Create new file at destination
                        await storageLocation.CreateFileAsync(dest,Windows.Storage.CreationCollisionOption.ReplaceExisting);
                        created = true;
                    }
                    catch (Exception e)
                    {
                        created = false;
                    }

    Where dest = String.Concat(ApplicationData.Current.LocalFolder.Path, @"\TestFile.jpg");

    My issue is that whenever it tries to read or create the file I get the following exception:

    The filename, directory name, or volume label syntax is incorrect. (Exception from HRESULT: 0x8007007B)

    Is there a reason this is happening? 


    Thursday, August 22, 2013 10:34 PM

Answers

  • CreateFileAsync's argument is the relative path to the file, not the full path.

    Assuming that storageLocation is ApplicationData.Current.LocalFolder then dest should be just "TestFile.jpg"

    --Rob

    Thursday, August 22, 2013 11:02 PM
    Moderator

All replies

  • CreateFileAsync's argument is the relative path to the file, not the full path.

    Assuming that storageLocation is ApplicationData.Current.LocalFolder then dest should be just "TestFile.jpg"

    --Rob

    Thursday, August 22, 2013 11:02 PM
    Moderator
  • Hello obece,

    this error is come Because you can put path of your image (testfile.jpg) instead of image name. you replace your image name (testfile.jpg) instead of dest.

    So you replace this code:

    await storageLocation.CreateFileAsync(dest, CreationCollisionOption.ReplaceExisting);

    to:

    await storageLocation.CreateFileAsync("test.jpg", CreationCollisionOption.ReplaceExisting);

    Friday, August 23, 2013 9:20 AM
  • Thanks!! That seems to work now!!
    Friday, August 23, 2013 5:26 PM