Xamarin Could not find a part of the path - c#

I am trying to save a bitmap to the Android storage.
My code looks like this:
var folderPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
var filePath = System.IO.Path.Combine(folderPath, "Pictures/profile_picture.png");
var stream = new FileStream(filePath, FileMode.Create);
bitmap.Compress(Bitmap.CompressFormat.Png, 100, stream);
stream.Close();
But the application enters break mode and it says:
System.IO.DirectoryNotFoundException: Could not find a part of the path.
Any help is appreciated.
Thanks in advance, Zoedingl.

"Pictures" directory is not created. So, you are getting "Could not find a part of the path" error. Change code as below or you can create directory for "Pictures" then use same code should work.
var filePath = System.IO.Path.Combine(folderPath, "profile_picture.png");
Code for creating directory
Directory.CreateDirectory(System.IO.Path.Combine(folderPath, "Pictures"));

Related

C# Filestream denied access

I'm trying to use C# to create a file, and read files back to fill out a rich text block. right now my problem is in creating/writing to the file.
FileStream fs = File.Create(#".\\tmp\" + fileName);
This is where I'm trying to write to. .\tmp\ exists, but when trying to write it it errors, saying
.\tmp\filename access is denied
The probably is that the user that is running the application probably doesn't have access to write to that directory. The easiest way to test that would be to run your application as administrator you should have access to write to that directory then.
You might also want to consider writing to the current directory no matter what user who is running your application should at the very least have access to that directory
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
Probably you don't have access to the relative path.
To get your assembly directory:
private static string AssemblyDirectory
{
get
{
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
}
Then
FileStream fs = File.Create(Path.Combine(AssemblyDirectory, fileName));
You are using a relative path which leads to a location which you don't have access to.
A possible solution could be to:
Create a folder C:/data and make sure you have read and write rights to that folder
change the code to
string fileName = "file.txt";
FileStream fs = File.Create(#"C:/data/" + fileName);
This should create a file under C:/data with the filename "file.txt", assuming you have the correct read and write rights.
If you want a relative path to the current user's root directory, use:
string currentUserDirectory =
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
This happened to me . I had had an anti-virus blocking access to any file when the writing or reading process happening from a C# program. I have just deactivated the anti-virus and the code worked like magic !

Access file from the app folder in uwp

Directory:
App1
- MainPage.xaml.cs
- Sample.xaml
im trying to do is getting the xaml content from the sample as a string but it doesnt work since it cant find the file:
var x = Path.GetFullPath(#"sample.xaml");
FileStream s = new FileStream(x, FileMode.Open);
How can I fix this?
I haven't tried this give it a try.
Save your file in a location eg (Assets Folder)Now, make sure that the
build action is set to Content.
var storageFile = await StorageFile.GetFileFromApplicationUriAsync(
new Uri("ms-appx:///Assets/youfile.xaml"));
The StorageFile you get is of course read-only, but it can be passed to any API that expects a StorageFile.
If you want to read you can try.
var result = storageFile.OpenReadAsync()
StorageFile Documentation
The source files (.xaml, .cs) are compiled, and in the deployed app they do not exist as physical files, so you can't open them this way.

Windows Phone 8.1 access file in Documents folder

I'm new to windows phone development, and not using silverlight or WPF. I copy the file "links.txt" into my Windows Phone folder at "\Documents\" and I want to access and get the content in the file, but I'm getting back as access denied error. I click on Package.appxmanifest file then select "Capabilities" tab, but I don't see "Documents Library Access" for me to check it. As matter fact I don't see any "... Library Access" showing. Below is the code:
string fileName = "\\Documents\\links.txt";
string parentPath = ApplicationData.Current.LocalFolder.Path;
string filePath = Path.Combine(parentPath, fileName);
StorageFile file = await StorageFile.GetFileFromPathAsync(filePath);
Any suggestion how can I read the file? thanks.
Updates:
It seems the code above does not work, but when using the code below and change the folder to "Music" instead of "Documents" then check the Capabilities for "MusicLibrary", it's working.
var folder = KnownFolders.MusicLibrary;
var file = await folder.GetFileAsync("links.txt");
var read = await FileIO.ReadTextAsync(file);
Thanks!
You are give the wrong filePath actual path is in this image
i try your code it shows same error so correct your file path
If you checked need Capability for Documents you should use KnownFolders class instead ApplicationData.Current.LocalFolder.Path.
For example:
StorageFolder storageFolder = KnownFolders.DocumentsLibrary;
StorageFile file = await storageFolder.CreateFileAsync("sample.dat", CreationCollisionOption.ReplaceExisting);

Can't use file stream obtained from skydrive to create BitmapImage

I'm writing a windows phone 8 app and I would like to let users use images saved to their skydrive in my app. The piece of code I'm having trouble with (I believe) is below.
StorageFile thefile = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync("b4b.png", CreationCollisionOption.ReplaceExisting);
Uri theuri = new Uri("ms-appdata:///local/b4b.png");
var thething = await client.BackgroundDownloadAsync(filepath, theuri); <--- line where program crashes
BitmapImage src = new BitmapImage();
src.SetSource((Stream)await thefile.OpenReadAsync());
WriteableBitmap image = new WriteableBitmap(src);
all the signing in and authentication stuff that the user needs to do is already done by this point and works as expected. When my program reaches the marked line, it suddenly crashes. The errors I receive are...
A first chance exception of type 'System.ArgumentException' occurred in Microsoft.Live.DLL
A first chance exception of type 'System.ArgumentException' occurred in mscorlib.ni.dll
A first chance exception of type 'System.ArgumentException' occurred in mscorlib.ni.dll
'TaskHost.exe' (CLR C:\windows\system32\coreclr.dll: Silverlight AppDomain): Loaded 'C:\windows\system32\en-US\mscorlib.debug.resources.dll'. Module was built without symbols.
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.ni.dll
Does anyone know how to fix this?
I inserted breakpoints to trace the program and it appears that the storage file is being made and the uri is correct, but when I try to download the file to it, the program gives me the error. I confirmed the filepath for the file on skydrive is also correct. If I try to use DownloadAsync() instead it appears to work but then the program crashes when I try to use the stream obtained from the skydrive file instead and gives the same error.
Any ideas? Because I can't figure out what could be wrong.
The file being downloaded is a png image.
EDIT: Solution Found
After some more research I found out that when you download files from skydrive a call for a files id...
filepath = result.id;
as above that does not give you the contents of the file. I didn't check what it was obtaining but I'd assume it was probably the metadata. To obtain the actual contents of the file you must add "/contents".
The correct path would then be
filepath = result.id + "/contents";
I edited my code as shown below and it now works perfectly.
StorageFile thefile = await Windows.Storage.ApplicationData.Current.LocalFolder.CreateFileAsync("b4b.png", CreationCollisionOption.ReplaceExisting);
Uri theuri = new Uri("ms-appdata:///local/b4b.png", UriKind.Absolute);
var thething = await client.DownloadAsync(filepath + "/content");
Stream stream = thething.Stream;
stream.Seek(0, SeekOrigin.Begin);
BitmapImage src = new BitmapImage();
src.SetSource(stream);
Hope this helps anyone having the same problem as I was!
From the #deboxturtle's update, as an answer for search sake:
After some more research I found out that when you download files from skydrive a call for a files id...
filepath = result.id;
as above that does not give you the contents of the file, you get file metadata as json. To obtain the actual contents of the file you must add /content to the id.
The correct path would then be
filepath = result.id + "/content";
I edited my code as shown below and it now works perfectly.
var filepath = result.id + "/content";
StorageFile thefile = await ApplicationData.Current.LocalFolder.CreateFileAsync(
"b4b.png", CreationCollisionOption.ReplaceExisting);
var thething = await client.DownloadAsync(filepath);
Stream stream = thething.Stream;
stream.Seek(0, SeekOrigin.Begin);
BitmapImage src = new BitmapImage();
src.SetSource(stream);

How to get the full Path of the file upload control in a string variable in asp .net?

I would like to get the full path of the file upload control in the string variable. The file may be stored in any location other than the root of the project. Anybody please help out.
The situation is:
string file = Path.GetFileName(ExcelFileUpload.FileName);
if (file.EndsWith(".xlsx"))
{
// Reading from a binary Excel file (format; *.xlsx)
FileStream stream = File.Open(file, FileMode.Open, FileAccess.Read);
It sounds like you're actually asking for the original path to the file on the client machine.
This is (a) useless (it's on a different computer) and (b) impossible to get (the browser doesn't tell you it).
What are you trying to do?
You could try somehting like this : (where MyFileUploader is your FileUpload control)
string fileBasePath = Server.MapPath("~/");
string fileName = Path.GetFileName(this.MyFileUploader.FileName);
string fullFilePath = fileBasePath + fileName;

Categories

Resources