I get error
The filename, directory name, or volume label syntax is incorrect. (Exception from HRESULT: 0x8007007B)
My code is
public async void ReadFile()
{
var path = #"F:\VS\WriteLines.xls";
var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
var file = await folder.GetFileAsync(path);
var readFile = await Windows.Storage.FileIO.ReadLinesAsync(file);
foreach (var line in readFile.OrderBy(line =>
{
int lineNo;
var success = int.TryParse(line.Split(';')[4], out lineNo);
if (success) return lineNo;
return int.MaxValue;
}))
{
itemsControl.Items.Add(line);
}
}
The error shows up at var file = await folder.GetFileAsync(path);
You cannot read a file from an arbitrary location on disk in a UWP App.
There are a couple of ways you can still accomplish your task:
You can add the WriteLines.xls file to your project and set it's build action to Content and Copy to output Directory to Copy if newer. You can then access the file with the code you have by simply replacing the "path" value to:
var path = #"WriteLines.xls"
More details here
If you need to be able to read any files from disk, you need to either use a FilePicker to select the file or copy the file in the Documents Folder and change the folder to:
var folder = KnownFolders.DocumentsLibrary;
More details here
You are asking for file with absolute path from application's local folder - hence it throws that error as you provide path that includes drive name.
In general UWP is very restrictive on where/how you can get files from - I don't think you can get it from absolute path in the sample (app needs more permissions to get to similar places). You can try StorageFile.GetFileFromPathAsync.
Detailed info on locations app can access - UWP apps on Windows 10: File access permissions.
Related
I am trying to read a text file named thedata.txt that has a list of words that I want to use in a hangman game. I have tried different ways, but I can't figure out where the file gets placed, if at all when the app runs. I added the file to my project, and I have tried setting the build properties to content, and then embedded resource, but can't find the file. I have made a Windows 10 universal app project. The code I tried looks like this:
Stream stream = this.GetType().GetTypeInfo().Assembly.GetManifestResourceStream("thedata.txt");
using (StreamReader inputStream = new StreamReader(stream))
{
while (inputStream.Peek() >= 0)
{
Debug.WriteLine("the line is ", inputStream.ReadLine());
}
}
I get exceptions.
I also tried to list the files in another directory:
string path = Windows.Storage.ApplicationData.Current.LocalFolder.Path;
Debug.WriteLine("The path is " + path);
IReadOnlyCollection<StorageFile> files = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFilesAsync();
foreach (StorageFile file2 in files)
{
Debug.WriteLine("Name 2 is " + file2.Name + ", " + file2.DateCreated);
}
I don't see the file there either...I want to avoid hard coding the list of names in my program. I'm not sure what the path that the file is placed.
the code is very simple, you just have to use a valid scheme URI (ms-appx in your case) and transform your WinRT InputStream as a classic .NET stream :
var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///thedata.txt"));
using (var inputStream = await file.OpenReadAsync())
using (var classicStream = inputStream.AsStreamForRead())
using (var streamReader = new StreamReader(classicStream))
{
while (streamReader.Peek() >= 0)
{
Debug.WriteLine(string.Format("the line is {0}", streamReader.ReadLine()));
}
}
For the properties of the embedded file, "Build Action" must be set to "Content" and "Copy to Ouput Directory" should be set to "Do not Copy".
You can't use classic .NET IO methods in Windows Runtime apps, the proper way to read a text file in UWP is:
var file = await ApplicationData.Current.LocalFolder.GetFileAsync("data.txt");
var lines = await FileIO.ReadLinesAsync(file);
Also, you don't need a physical path of a folder - from msdn :
Don't rely on this property to access a folder, because a file system
path is not available for some folders. For example, in the following
cases, the folder may not have a file system path, or the file system
path may not be available. •The folder represents a container for a
group of files (for example, the return value from some overloads of
the GetFoldersAsync method) instead of an actual folder in the file
system. •The folder is backed by a URI. •The folder was picked by
using a file picker.
Please refer File access permissions for more details.
And Create, write, and read a file provides examples related with File IO for UWP apps on Windows 10.
You can retrieve a file directly from your app's local folder by using an app URI, like this:
using Windows.Storage;
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync("ms-appdata:///local/file.txt");
I want to create a folder named "TestFolder" on local folder of my uwp app. And need to upload files to it. It works fine on my system when I first manually create a folder((TestFolder) on local folder. but when I create app package for the project and tried to run it in another windows pc. It throws an error "System cant find the file specified". How can I resolve it?
StorageFolder appFolder = await ApplicationData.Current.LocalFolder.GetFolderAsync("TestFolder");
if (appFolder == null)
{
//Create folder
appFolder = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFolderAsync("TestFolder");
}
i would replace this few lines by this single line
var appFolder = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFolderAsync("TestFolder", Windows.Storage.CreationCollisionOption.OpenIfExists);
You can modify your code this way to get your desired result,
StorageFolder appFolder = await ApplicationData.Current.LocalFolder.GetFolderAsync("TestFolder");
if (appFolder == null)
{
//Create folder
appFolder = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFolderAsync("TestFolder",CreationCollisionOption.OpenIfExists);
}
I am creating files and folders using DownloadsFolder methods.
I'd like to get the parent folder as a StorageFolder instance so I can list and manipulate all the items in the app's downloads folder.
I've tried GetParentAsync() from a known StorageFile, but the return is null.
StorageFile sf = await DownloadsFolder.CreateFileAsync("testMarker");
StorageFolder dlFolder = await sf.GetParentAsync();
Is there any method to access this folder programmatically?
It seems your app doesn't has permission to read the Downloads directory. We can use GetParentAsync to get a parent the app has permissions for, but not to get folders the app doesn't have permissions in.
If we add the Music Library, Pictures Library and Videos Library to the Capabilities of the appxmanifest that we can use the GetParentAsync method to get the parent folder as a StorageFolder in these folder.
If you create a file or folder in the Downloads folder, we recommend that you add that item to your app's FutureAccessList so that your app can readily access that item in the future.
For more info, please refer the Locations Windows Store apps can access.
So if you want to get the other folders and files in DownloadsFolder, you should be able to Open files and folders with a picker.
It looks like this isn't possible - You can try to get the storage folder by using the Path with System.IO directly like so
var downloadPath = System.IO.Path.GetDirectoryName(storageFile.Path);
var downloadsFolder = await StorageFolder.GetFolderFromPathAsync(downloadPath);
But GetFolderFromPathAsync seems to throw an exception, I think windows just won't give you a reference to this folder.
A solution is to use a folder picker ONCE to pick the downloads folder (as part of the inital setup of the app). After that the app has full access to the downloads folder.
This code needs to be called once:
var folderPicker = new FolderPicker();
folderPicker.SuggestedStartLocation = PickerLocationId.Downloads;
folderPicker.FileTypeFilter.Add("*");
StorageFolder folder = await folderPicker.PickSingleFolderAsync();
if (folder != null)
{
// Application now has read/write access to all contents in the picked folder
// (including other sub-folder contents)
Windows.Storage.AccessCache.StorageApplicationPermissions.
FutureAccessList.AddOrReplace("DownloadFolderToken", folder);
var messageDialog = new MessageDialog("Download folder: " + folder.Name);
await messageDialog.ShowAsync();
}
else
{
var messageDialog = new MessageDialog("Operation cancelled");
await messageDialog.ShowAsync();
}
This code can then be used to directly access the downloads folder:
var downloadsFolder = await StorageApplicationPermissions.FutureAccessList.GetFolderAsync("DownloadFolderToken");
IReadOnlyList <StorageFile> fileList = await downloadsFolder.GetFilesAsync();
StringBuilder outputText = new StringBuilder();
outputText.AppendLine("Files:");
foreach (StorageFile file in fileList)
{
outputText.Append(file.Name + "\n");
await file.DeleteAsync();
}
I using .Net Framework 4.0; VS 2015; Ionic.Zip.Reduced (DotNetZip.Reduced) v1.9.1.8. When I try to add a folder to the archive get an exception with the text:
The path is too long
Sample code:
using (var zipFile = new ZipFile(zipFilePath))
{
zipFile.UseZip64WhenSaving = Zip64Option.AsNecessary;
zipFile.AlternateEncodingUsage = ZipOption.Always;
zipFile.AlternateEncoding = Encoding.UTF8;
zipFile.ParallelDeflateThreshold = -1;
var dirPath = #"C:\AAAAAAAAAAA\AAAAAA\AAAAAAAAAAAAAAA\AAAAAAAAA\AAAAAAAAAAAAA\AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\";
zipFile.AddDirectory(dirPath); <-Exception
zipFile.Save();
}
In the folder is a file named: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA.zip
As a result of an error:
The path is too long
Rewritten in the file-based addition to the archive (using a relative path):
using (var zipFile = new ZipFile(zipFilePath))
{
zipFile.UseZip64WhenSaving = Zip64Option.AsNecessary;
zipFile.AlternateEncodingUsage = ZipOption.Always;
zipFile.AlternateEncoding = Encoding.UTF8;
zipFile.ParallelDeflateThreshold = -1;
var dirPath = #"C:\AAAAAAAAAAA\AAAAAA\AAAAAAAAAAAAAAA\AAAAAAAAA\AAAAAAAAAAAAA\AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\";
Directory.SetCurrentDirectory(dirPath);
var files = Directory.GetFiles(dirPath, "*", SearchOption.AllDirectories).ToArray();
foreach (var fullFilePath in files)
{
var fileName = Path.GetFileName(fullFilePath);
var relatedPath = fullFilePath.Substring(0, fullFilePath.LastIndexOf(fileName, StringComparison.InvariantCultureIgnoreCase)).Replace(zipDir, "");
var relatedFilePath = Path.Combine(relatedPath, fileName);
zipFile.AddFile(relatedFilePath); <-Exception
}
zipFile.Save();
}
The error is the same:
The path is too long
I tried to call Path.GetDirectoryName() method, but it also returns an error:
The specified path, file name, or both are too long. The fully
qualified file name must be less than 260 characters, and the
directory name must be less than 248 characters.
I found a lot of solutions but to get to work and did not work (because of the specifics of the application to the new version Framework'a can not go).
Use Framework 4.6.2. Set UseLegacyPathHandling = false option in App.Config or Switch.System.IO.UseLegacyPathHandling = false; Switch.System.IO.BlockLongPaths = false
With the mention of a Group Policy and the inclusion of the option Configuration> Administrative Templates> System> Filesystem> Enable NTFS long paths, or to enable the option via the manifest <ws2:longPathAware>true</ws2:longPathAware>
Use the prefix \\?\ In the path (I understand that for the new version of Framework)
Convert path to the file in 8.3 format using GetShortPathName function .... (Error remains)
Maybe someone faced such problem. I will be glad to any advice. Thanks.
If your path is too long there's not much you can do about it. Even if you can move Windows limits a step further your application won't work well on a non ad-hoc configured system in that scenario.
You can workaround copying the files you have to work with to a temp folder like C:\temp and add the files to the archive from there.
You can even mimic the same folder tree structure with directory names composed of only 1 or 2 letters and then map the complete (but really shorter) directory path to the original path somewhere (on a file for example), so that you can rebuild the original folder tree structure with the same names later on.
Wow, is this way more complicated than it needs to be. Can someone explain to me why the following code works:
string stringToWrite = "SomeStuff";
Windows.ApplicationModel.Package package = Windows.ApplicationModel.Package.Current;
Windows.Storage.StorageFolder installedLocation = package.InstalledLocation;
var files = await installedLocation.GetFilesAsync();
foreach (Windows.Storage.StorageFile sf in files)
{
if (sf.Name.Equals("log.txt"))
{
await FileIO.AppendTextAsync(sf, stringToWrite);
}
}
And yet the following fails with AccessDenied:
Windows.ApplicationModel.Package package = Windows.ApplicationModel.Package.Current;
Windows.Storage.StorageFolder installedLocation = package.InstalledLocation;
var log = await installedLocation.GetFileAsync("log.txt");
await FileIO.AppendTextAsync(log, stringToWrite);
The only difference is looping through the files returned by the GetFilesAsync method vs getting the file by name. By the way, getting the file by name works because if I misspell log.txt in GetFileAsync, I get an exception.
Very confusing....
You should not be using your installed location to write any files. It is supposed to be read-only as per MSDN: File Access/Permissions in Windows Store Apps:
The app's install directory is a read-only location. You can’t gain access to the install directory through the file picker.
You should be using either the Local, Roaming, or Temporary storage locations.
See this link: MSDN: Quickstart Local Application Data