Blog Archives

directory info get files show directories C# .Net

There is sometimes a misunderstanding between a “file” and a “folder” in filesystems. In C# .Net the following is often used to list all “files” in a folder.

DirectoryInfo di = new DirectoryInfo("yourpath");

foreach (FileInfo fi in di.GetFiles()) {
//do work
}

However, even though you can specify to search containing subdirectories, the above function will not inherently list folders. If you are looking for the equivalent to “dir” or “ls”, instead use “GetFileSystemInfos()”.

            DirectoryInfo di = new DirectoryInfo("yourpath");
		
//note the difference here with getfilesysteminfos
		foreach (dynamic d in di.GetFileSystemInfos()) {
}

Note the usage of dynamic in the above example compared to the first example. This avoids any potential issues with inheritance and choosing the right class for your temp iterator variable for unboxing etc.

Advertisements

call parent page from user control to invoke page methods

To illustrate how we will accomplish this using Reflections, we will be using repeater in a user control within a page.

Your repeater loads data easily within the user control code behind, but you want to utilize the “ItemCommand” event of the repeater to update some values on the page page.

Here’s how this can work:

Parent Page:

//the name doesn't have to match the corresponding method on user control but makes code more readable
    public void rptRepeaterName_ItemCommand(object sender, RepeaterCommandEventArgs e)
    {

        try
        {
            if (e.CommandName == "commandname")
            {
               //do some work on data then call update on parent page to reload data and show within a modal

                ParentPage_UpdatePanel.Update();
                ParentPage_ModalPopup.Show();

            }
        }
        catch (Exception ex)
        { 
//do something
            throw new ApplicationException(ex.ToString());
        }

    }

User Control:

    protected void rptRepeaterName_ItemCommand(object sender, RepeaterCommandEventArgs e)
    {
        MethodInfo mi = this.Page.GetType().GetMethod("rptRepeaterName_ItemCommand", BindingFlags.Public | BindingFlags.Instance);

//note we specify parent page as the object and pass in a new object representing our repeater and carrying its parameters
        if (mi!=null) mi.Invoke(this.Page, new Object[] {sender,e });

    }

Voila! Behold the power of Reflections! 8)

In particular, one of the references below (thanks Bruce Barker!) helped me come to this answer, however, the exact code he presents will result in a “Non-static method requires a target” error.

To avoid this error, make sure you always pass in the object when invoking a method that is non-static (within a class that is instantiated).

To learn more about reflections and how it works search my blog for other examples, and visit MSDN for a good overview.

References
Velocity Reviews, http://www.velocityreviews.com/forums/t71075-invoke-methods-on-the-parent-page-from-the-user-control.html
Reflection Overview (MSDN), http://msdn.microsoft.com/en-us/library/f7ykdhsy%28v=vs.71%29.aspx

the savedstate dictionary does not contain the expected values and might have been corrupted

This is a common error with custom action development, especially if you are working with your own custom installer class, not using wix or some other windows installer derivative.

Since many of the Google entries for this were unresolved, thought I would give it a quick tackle. 😉

First Solution

Throw an exception.

This solution seems to be the most popular among Google results.. You can throw a new Exception or InstallException, but this creates an unsightly error message which you would not want your users to see.

 public override void Install(System.Collections.IDictionary stateSaver)
        {
            Process proc = new Process();
            string path = Path.GetDirectoryName(System.Reflection.Assembly.GetAssembly(typeof(InstallHelper)).Location);
            proc.StartInfo.FileName = path + "\\somesubprocess.exe";
            
//note ProductCode argument is an installer variable
            proc.StartInfo.Arguments = "\"" + this.Context.Parameters["action"] + "\" \"" + this.Context.Parameters["ProductCode"] + "\" \"" + this.Context.Parameters["src"];

            proc.Start();

//optional showwindow code to force window into foreground
 IntPtr msiwindowhandle;
            msiwindowhandle = FindWindow("#32770", InstallTitle);
            ShowWindow(msiwindowhandle, 0);

if (proc.ExitCode == 1)
            {
throw new InstallException("Installation cancelled. Any changes made will now rollback.");
                Process.GetCurrentProcess().Kill();
                if (stateSaver == null)
                {
                    stateSaver = new Dictionary<string, string>();
                    //Add some default values to the dictionary here...if you dare..
                }

                base.Uninstall(stateSaver);
                base.Rollback(stateSaver); //will give you article title error
            }
            else
            {
                base.Install(stateSaver);
            }
}

The code above was a bit much to simply explain the concept of throwing a new “InstallException”, but does illustrate there are definitely things you can do from your custom action bootstrap DLL which have room for error (like spawning a sub process!).

The Alternative

…is to simply click/call the cancel button for the base installer from your custom action code.

Since the msi installer process is separate from your bootstrap custom action DLL, you will have to use some old school tricks to work around the constraints.

Simply import User32.dll and call the “SendMessage” function, passing in the handle of cancel button. See code example below:

using System.Runtime.InteropServices;

[DllImport("User32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr SetActiveWindow(IntPtr hWnd);

        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, ShowWindowCommands nCmdShow);

        enum ShowWindowCommands : int
        {

            Hide = 0,
            Normal = 1,
            ShowMinimized = 2,
            Maximize = 3,   
            ShowMaximized = 3,
            ShowNoActivate = 4,
            Show = 5,
            Minimize = 6,
            ShowMinNoActive = 7,
            ShowNA = 8,
            Restore = 9,
            ShowDefault = 10,
            ForceMinimize = 11
        }

public static string InstallTitle = "AppbarTitle"; //used to get the window handle

  public override void Install(System.Collections.IDictionary stateSaver)
        {
ShowWindow(msiwindowhandle, ShowWindowCommands.Show);
                IntPtr cancelbuttonhandle;
                const int BM_CLICK = 0x00F5;
                msiwindowhandle = FindWindow("#32770", InstallTitle);
                cancelbuttonhandle = FindWindowEx(msiwindowhandle, IntPtr.Zero, "Button", "Cancel");
                SetActiveWindow(msiwindowhandle); //necessary for button click to fire
                SendMessage(cancelbuttonhandle, BM_CLICK, IntPtr.Zero, IntPtr.Zero);
}

The MSDN Social article listed in my references below comes up in Google rankings high for the error the OP received when calling the Rollback method manually. To track any social momentum my solution may possibly gain, refer to this link.

References
MSDN Social, http://social.msdn.microsoft.com/Forums/en/winformssetup/thread/ea7d09d1-2812-4f04-b6ed-e7293f2a2d75
wix, http://wix.sourceforge.net/
MSDN, “Installer.Rollback Method”, http://msdn.microsoft.com/en-us/library/system.configuration.install.installer.rollback.aspx
MSDN, “Installer.BeforeRollback Method”, http://207.46.16.248/en-us/library/system.configuration.install.installer.beforerollback%28VS.80%29.aspx