1

I have a file in IsolatedStorage. If the file exists, I want to redirect to the login page or Create Account page.

If the file doesnt exist, the app goes to the Create page, a password is created and saved, and the app redirects to the Login page. However, if the file in IsolatedStorage exists, it won't direct.

private void fileExists()
        {
            IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
            if (store.FileExists("passwordFile"))
            {
                //NavigationService.Navigate(new Uri("/Login.xaml", UriKind.Relative));
                MessageBox.Show("Should be redirecting here");
            }

            else
            {
                MessageBox.Show("Welcome. Please create an account. Ensure that you remember your password!");
            }
        }

The actual message does show so it's being called and if a file does not exist, the else is executed so my logic is sound.

The FileExists() function is called here.

public MainPage()
        {
            InitializeComponent();
            fileExists();
        }

The other redirect happens here

if ((password1.Password == password2.Password) & (password1.Password.Trim().Length > 0 || password2.Password.Trim().Length > 0))
            {
                byte[] PasswordByte = Encoding.UTF8.GetBytes(password1.Password);
                byte[] ProtectedPassword = ProtectedData.Protect(PasswordByte, null);
                this.WritePasswordToFile(ProtectedPassword);

                NavigationService.Navigate(new Uri("/Login.xaml", UriKind.Relative));
            }

The error is a System.NullReferenceException but was not handled in user code.

Jibran Khan
  • 3,236
  • 4
  • 37
  • 50
Chris O'Brien
  • 372
  • 6
  • 25
  • real offtopic here, please hash or encrypt your passwordFile, with some fancy coding and modded WP8 it easily to access the isolatedStorage – EaterOfCode Mar 22 '13 at 10:41
  • 1
    I will be, it's literally just the start! I just started C# and WP yesterday and encryption is a big part of the project, but not something that I will be looking at for a few weeks. Thanks though! – Chris O'Brien Mar 22 '13 at 10:44

3 Answers3

1

Have tried to call the file exist check on MainPage load ? That can be storage preparing issue even if it is executing. Secondly if you can mention where the exact exception is occuring. Also do check this link that might help you.

Community
  • 1
  • 1
Jibran Khan
  • 3,236
  • 4
  • 37
  • 50
  • The file definitely exists. If I comment out the navigation part, it returns true and displays a message box, so the file is definitely there. The exception is occurring in the fileExists() function at the navigation line (commented out above) I will have a look through that link, seems good, but logically, I can't figure out why it should be causing an exception somewhere and not elsewhere, especially when other code runs fine. – Chris O'Brien Mar 22 '13 at 10:47
  • Also do try to put the the check code in loaded event of the page. – Jibran Khan Mar 22 '13 at 10:53
1

The problem is that the NavigationService is still null, you can verify that by putting a breakpoint on the redirect line, put the same code in the MainPage.Loaded event and it will work then, (I expect it to work then)

as I expect this is only a redirect page you can check the file in the initialization and save uri to redirect in the class and redirect when the page is loaded

EaterOfCode
  • 2,202
  • 3
  • 20
  • 33
0

I needed to move the fileExists() from the constructor to a new function.

protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            fileExists();
        }
Chris O'Brien
  • 372
  • 6
  • 25