Click here to Skip to main content
15,867,308 members
Articles / Mobile Apps / Windows Phone 7
Technical Blog

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

Rate me:
Please Sign up or sign in to vote.
4.95/5 (4 votes)
20 Mar 2013Ms-PL1 min read 12.5K   6   2
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.

C#
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.

C#
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.

C#
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:

C#
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

C#
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.

C#
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.

C#
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

C#
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:

This article was originally posted at http://khason.net?p=2219

License

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


Written By
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? Pin
Member 1020231311-Aug-13 1:31
Member 1020231311-Aug-13 1:31 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA12-Apr-13 19:03
professionalȘtefan-Mihai MOGA12-Apr-13 19:03 

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

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