My UWP app won't read a txt file [duplicate] - c#

This question already has answers here:
Windows 10 Universal App File/Directory Access
(6 answers)
Closed 6 years ago.
I was wondering if anyone would know how to get an Universal Windows app to read a txt file.
StreamReader lezer = new StreamReader(#"C:\Users\LotteDiesveld\Documents\Visual Studio 2015\Projects\Jaar 1\Periode 2\OIS 12\Eindopdracht\Personen.txt");
tbPersonen.Items.Add(lezer.ReadToEnd());
I've also tried this
StorageFile file = await StorageFile.GetFileFromApplicationUriAsync("ms-appx:///reserveringen.txt")
I know multiple answers have been given on this subject, but none work, because I keep getting an error. This error is either "Cannot convert type string to type uri" or "cannnot convert type string to type stream".

A UWP app runs in a sandboxed environment and has no permissions to read a file from anywhere on the hard drive. If you want to read a file in the user's Documents Library you should either let the user select the file using a picker or declare the folder in the application's manifest. Please refer to Jerry Nixon's blog post for more information: http://blog.jerrynixon.com/2012/06/windows-8-how-to-read-files-in-winrt.html
Please also refer to the following similar question:
Windows 10 Universal App File/Directory Access
For information about how to read a file that is distributed along with your app, please see the answer here:
how to read a text file in windows universal app

Related

C# (WPF) - Getting Library folder paths in App.config [duplicate]

This question already has answers here:
Log4net output to My Documents
(3 answers)
Closed 7 years ago.
I'm looking to place some logfiles in the user's documents folder. The log path for my log4net logger is configured in the App.config XML, so I can't programmatically determine the path using the usual methods. Can the My Documents library be accessed by an environment variable, like pre-Windows 7 documents folders?
Is this what you are looking for?
string path = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
Edit
If you logging other than log4net. You need not store the log file path in the app.config in that case. because whatever you write in app.config you will end up overriding it. So it will be misleading and useless. If you want to pickup the MyDocuments location my suggestion is to fetch it in the code and wire it up with your logging module.
If you are using log4netuse the suggestion by #derpirscher - Log4net output to My Documents
Have a look at Environment.GetFolderPath (MSDN)
Now, as you clarified it's for log4net have a look at the following question
Log4net output to My Documents

Get current path in Windows 8 WPF app

I'm trying to bind images for my Windows 8 app but I currently am struggling with trying to know where the application diretory is.
I've read many threads about this but the Windows 8 namespaces do not contain the tools I've seen such as:
Directory.GetDirectory()
AppDomain
Assembly.GetExecutingAssembly()
etc...
If anyone can tell me how to know where I should load my images from, I would be very grateful.
I hope here by Windows 8 app you mean Windows 8 (Silverlight) app.
Well by default it doesn't provide you access to current directory. The only thing you can do is to get access to temporary directory and use that storage:
StorageFolder temp = Windows.Storage.ApplicationData.Current.TemporaryFolder;
For further reference please consult to:
http://lunarfrog.com/blog/winrt-folders-access

Define virtual system variables for children applications [duplicate]

This question already has answers here:
Set environment variables for a process
(2 answers)
Closed 7 years ago.
I am currently working on a program that would run other application under given virtual environment. I am running another application like so:
Process app = new Process();
app.StartInfo.FileName = #"W:\path\to\app\some.exe";
app.EnableRaisingEvents = true;
app.Start();
Now I have faced an issue to change some system variables for application to be run. I have googled for this and could not found the solution. Anybody has any idea how to solve this problem, please help me?
Thanks.
Update
For example I want to set another PATH, JAVA_HOME, AppData variable for child application. Application could be: Google Chrome, Notepad++ or simple .bat script for command line.
assuming you need to change path variable
string pathvariable=Environment.GetEnvironmentVariable("Path");
Environment.SetEnvironmentVariable("Path",pathvariable+";"+"*you new value of path variable*");
if your exe file works fine when you access it directly then you dont need to change the your system variable.. if you change your working directory while accessing the exe file then i guess it will work

How to get Application data folder? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can i get the path of the current user’s “Application Data” folder?
Windows XP Application Data Folder?
I have to save some settings in application data but, when i use something as "#C:\Documents ..." someone can run windous on D:\ So how to get that directory ??
You can use Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); ...
And there's exaple, how you can use it:
string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
and it returns something like C:\\Users\\UserName\\ApplicationData
and you can use Environment.SpecialFolder.Desktop too so you can get to desktop of actual user...
see the code at the end of this:
http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx
Look at this MSDN entry to get the application data directory, Environment.SpecialFolder.
What I used to do is use Evironment.SystemDirectory and then break that down depending on what I need. But if you are worried about the drives then use the DriveInfo class by doing DriveInfo.GetDrives()

server.mappath null [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
HttpContext.Current.Server null
I have three projects in my ASP .NET solution:
a class library
a web application
a windows service
I have a folder in my web application with an XML file. An XMLreader from class library function needs to be populated with this XML file. I have added this:
var reader = XmlReader.Create(HttpContext.Current.Server.MapPath("~/TestDevice/Data.xml"), settings);
When this function is called from we application, It works.
When this function of class library is called from windows application, I get null reference at HttpContext.Current.Server because it is not available in windows application. I don't want to give hardcode path although that works for both windows and web application. Can I use some don't function to access files from different projects or any alternate of server.mappath. Please suggest solution
Thanks
System.AppDomain.CurrentDomain.BaseDirectory should do the trick.
http://msdn.microsoft.com/en-us/library/system.appdomain.basedirectory.aspx
Gets the base directory that the
assembly resolver uses to probe for
assemblies.
HttpContext.Current.Server.MapPath alternative in windows application
file path using C#
HttpContext.Current.Server null
Asking the same basic question four different times isn't going to change the fact that you cannot use a Windows Service to access the web application path and the web app isn't going to know where your service is.
Use a configuration file to define where your data will be stored and provide the same info to both - or use some other storage for your data like a database which has a published location.
The simplest solution is probably to pass the path to the xml file into that function, rather than making the function itself clever enough to figure it out- then your web app could use the HttpContext method, and the Winforms app would use a regular path. There is no equivalent of the MapPath method for WinForms or windows services.

Categories