Avatar of elorc
elorc
Flag for United States of America asked on

Accessing files and handling errors on Windows 8 with VS2012/C# Metro app

I'm brand new to programming for Metro with Visual Studio 2012, and so far the process has been incredibly frustrating. I'm trying to write a simple app that opens a text file, changes it, and saves it. Easy, I'd expect, but apparently that's not the case! That, or I'm missing something. :)

1. My error handling sucks. I was trying to pop up a MessageDialog to notify the user of any exceptions that occur, but I learned that you can't have await (which means that you can't properly display the MessageDialog) within a catch block. I put together the solution shown below, but it's bulky and ugly. Surely there's a better way?

                        string ErrorMessage = "";
                        try
                        {
                            // do stuff here
                        }
                        catch (Exception ex)
                        {
                            ErrorMessage = ex.Message;
                        }
                        if (ErrorMessage != "")
                        {
                            MessageDialog mError = new MessageDialog(ErrorMessage, "Error");
                            await mError.ShowAsync();
                            ErrorMessage = "";
                        }

Open in new window





2. I can't seem to open files. At all. When I try something like this:

StorageFile file = await StorageFile.GetFileFromPathAsync(filePath);

Open in new window


It doesn't matter where the file is located -- c:\temp, My Documents, etc. -- the app throws an "access denied" exception. What am I missing?
Windows 8C#

Avatar of undefined
Last Comment
Sammy

8/22/2022 - Mon
McKnife

Hi.

I cannot help on 1, but maybe on 2). Please download procmon and monitor the access denials. in procmon, start logging, produce a denial, stop logging and search the log for "access denied" - this will show you if it's really at the file system/ACL level and what account is producing that error.
ASKER CERTIFIED SOLUTION
Sammy

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
elorc

ASKER
@Sammy: The strange thing is that this code works:

await FileIO.WriteTextAsync(selectedFile, ContentTextBox.Text);

Open in new window


Where selectedFile is the StorageFile object returned by a FilePicker. That file is located in my Documents folder. However, referencing the same exact file from the same app, this line throws an access denied error:

StorageFile file = await StorageFile.GetFileFromPathAsync(filePath);

Open in new window


It doesn't really make sense to me why it's behaving like that.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
elorc

ASKER
AH! I see! Okay, this is starting to make more sense now. It's weird having to figure out all of these little things. I'm so used to doing things the way I used to in previous versions of Windows.

On a somewhat unrelated note... I noticed that System.Security.Principal no longer has NTAccount or SecurityIdentifier. Are their functions available from some other component in .NET now? I had used them in a tool I had written in VS 2010. I'm trying to re-create the tool in VS2012 for Windows 8, and noticed that they're not there anymore.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Sammy

It still exists in framework 4.5(Desktop apps) but Not in Windows 8 Apps
http://msdn.microsoft.com/en-us/library/system.security.principal.ntaccount.aspx