Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Recursive delete from IsolatedStorage and other time savers for Windows Phone development

, 20 Mar 2013 Ms-PL
Rate this:
Please Sign up or sign in to vote.
Recursive delete from IsolatedStorage and other time savers for Windows Phone development.

As you have probably noticed, I returned to the consulting field. So I started to publish here again. Today, we’ll speak about some quick extension methods which help me to work with Windows Phone. We’ll start from IsolatedStorageFile (frankly, I do not know why they called it "File").

First of all, let’s make a method to delete the directory recursively, including files inside.

public static void DeleteDirectory(this IsolatedStorageFile store, string dir, bool recursive) { 
    if (store.DirectoryExists(dir)) { 
        if (recursive) { 
            foreach (var directory in store.GetDirectoryNames(Path.Combine(dir, "*"))) { 
                store.DeleteDirectory(directory, true); 
            } 
            foreach (var file in store.GetFileNames(Path.Combine(dir, "*"))) { 
                store.DeleteFile(Path.Combine(dir, file)); 
            } 
        } 
        store.DeleteDirectory(dir); 
    } 
}

Usage rather straight-forward (like regular IsolatedStorageFile.DeleteDirectory method but with additional parameter.

var store = IsolatedStorageFile.GetUserStoreForApplication();
store.DeleteDirectory(“MyDirectory”, true);

With recursive==true it will delete all inside the directory and the directory itself, without, will work exactly like the original method.

As known. Windows Phone API works only with XDocuments and Xml.Linq, which is good thing, but sometimes, it is nasty getting a value of the attribute you not sure us there. Thus I wrote a method to make the syntax cleaner.

public static string GetAttribute(this XElement element, string name) { 
    if (element.HasAttribute(name)) return element.Attribute(name).Value; 
    else return string.Empty; 
} 
public static bool HasAttribute(this XElement element, string name) { 
    return element.HasAttributes && element.Attribute(name) != null; 
}

Usage is simple:

var items = itm.Descendants("item").Where(e => e.GetAttribute("type") != "default"); 
    foreach (var item in items) { 
        var pid = item.GetAttribute("id"); 
… 
}

Another problem is the complicated syntax of visual tree manipulations. Thus I wrote some handy methods to handle it.

Check whether the tree contains an object

public static bool IsInVisualTree(this DependencyObject element, DependencyObject treeRoot) { 
    return element.GetVisualAncestors().Contains(treeRoot); 
}

Getting all visual ancestors our of the visual tree on the phone.

public static IEnumerable<DependencyObject> GetVisualAncestors(this DependencyObject element) { 
    DependencyObject candidate = null; 
    while (element != null) { 
        candidate = VisualTreeHelper.GetParent(element); 
        if (candidate == null) { 
            var asFe = element as FrameworkElement; 
            if (asFe != null) 
                candidate = asFe.Parent; 
        } 
        element = candidate; 
        if (element != null) 
            yield return element; 
    }
}

…and descendants for the element inside the tree.

public static IEnumerable<DependencyObject> GetVisualDescendants(this DependencyObject element) { 
    int childCount = VisualTreeHelper.GetChildrenCount(element); 
    for (int i = 0; i < childCount; i++) { 
        var child = VisualTreeHelper.GetChild(element, i); 
        yield return child; 
        foreach (var descendant in child.GetVisualDescendants()) { 
            yield return descendant; 
        } 
    } 
}

Here are some usage examples

if (originalSource.GetVisualAncestors().OfType<ItemBase>().Any()) { … } 
… 
itemsHost = this.GetVisualDescendants().OfType<Panel>().Where(p => p is PanelBase).FirstOrDefault()

Since, today I have a number of projects on Windows Phone, I’ll, probably, add some handy methods on going. As for you, you can put those together inside my golden sugar collection and work easier.

Be good people and have a nice day.

Related posts:

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

Share

About the Author

Tamir Khason
Architect Better Place
Israel Israel
Hello! My name is Tamir Khason, and I am software architect, project manager, system analyst and [of course] programmer. In addition to writing big amount of documentation, I also write code, a lot of code. I used to work as a freelance architect, project manager, trainer, and consultant here, in Israel, but recently join the company with extremely persuasive idea - to make a world better place. I have very pretty wife and 3 charming kids, but unfortunately almost no time for them.
 
To be updated within articles, I publishing, visit my blog or subscribe RSS feed. Also you can follow me on Twitter to be up to date about my everyday life.

Comments and Discussions

 
Question"recursive delete" box is check - leave it alone? PinmemberMember 1020231311-Aug-13 2:31 
GeneralMy vote of 5 PinmemberMihai MOGA12-Apr-13 20:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411019.1 | Last Updated 20 Mar 2013
Article Copyright 2013 by Tamir Khason
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid