Link to home
Start Free TrialLog in
Avatar of elorc
elorcFlag 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?
Avatar of McKnife
McKnife
Flag of Germany image

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
Avatar of Sammy
Sammy
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 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
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of 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.
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