0

When a button is clicked in our application, it downloads a file from the server to the client and opens it for the user to view. We allow the user to select the application to open that file with, but the standard box offers an "Always use the selected program to open this kind of file" option. Unofrtunately ticking this makes no difference and the .rtf file defaults to opening in Word again next time.

How do we get this setting to store and retreive correctly?

The code that we're using to display this window is as follows:

//Ask the user what application they want to open the file in.
if (strFileName != "" && File.Exists(strFileName))
{
    // Call Windows "Open With" dialog
    CoreUtilities.ShowOpenFileDialog(strFileName);
}

Many thanks

Colin

Colin
  • 1,141
  • 1
  • 9
  • 9
  • 1
    I don't think we can help you. You're using a 3rd party file-open dialog (CoreUtilities.ShowOpenFileDialog). unless you supply us with the relevant code - it's impossible for us to give u any tips at all.. – Shai Dec 22 '11 at 11:13
  • @Shai - it may be their own class wrapping a standard .NET component? – Adam Dec 22 '11 at 11:15
  • Basically you are asking how to save user settings: [What is the best way to store user settings for a .NET application?](http://stackoverflow.com/questions/26369/what-is-the-best-way-to-store-user-settings-for-a-net-application) – Paolo Moretti Dec 22 '11 at 11:17

3 Answers3

0

Here's a code snippet that calls Windows's Open with... dialog,

Unless CoreUtilities.ShowOpenFileDialog is already implemented using this approach, maybe you should give it a hit:

[Serializable]
public struct ShellExecuteInfo
{
    public int Size;
    public uint Mask;
    public IntPtr hwnd;
    public string Verb;
    public string File;
    public string Parameters;
    public string Directory;
    public uint Show;
    public IntPtr InstApp;
    public IntPtr IDList;
    public string Class;
    public IntPtr hkeyClass;
    public uint HotKey;
    public IntPtr Icon;
    public IntPtr Monitor;
}

// Code For OpenWithDialog Box

[DllImport("shell32.dll", SetLastError = true)]
extern public static bool 
       ShellExecuteEx(ref ShellExecuteInfo lpExecInfo);

public const uint SW_NORMAL = 1;

static void OpenAs(string file)
{
    ShellExecuteInfo sei = new ShellExecuteInfo();
    sei.Size = Marshal.SizeOf(sei);
    sei.Verb = "openas";
    sei.File = file;
    sei.Show = SW_NORMAL;
    if (!ShellExecuteEx(ref sei))
        throw new System.ComponentModel.Win32Exception();
}
Shai
  • 25,159
  • 9
  • 44
  • 67
0

There is a dirty hack, but I don't know if you'll like it :) You could delete stored assoc in registry before calling CoreUtilities.ShowOpenFileDialog() every time. Here's the path in registry

HKEY_CURRENT_USER/Software/Microsoft/Windows/CurrentVersion/Explorer/FileExts

Or you can try running

System.Diagnostics.Process.Start(path);

This will always use the default program. Or show the open with dialog when needed (when there's no default program associated)

Elastep
  • 3,272
  • 1
  • 14
  • 16
-1

Have you tried opening the file using the "open" verb?

    public static void displayLabel(string labelFileName)
    {
        System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(labelFileName);
        info.UseShellExecute = true;
        info.Verb = "open";
        System.Diagnostics.Process.Start(info);
    }

I use the code above to open up files. It will open the file with the default application you have assigned for the given extension. For example if called with a filename of .pdf it would open in Acrobat, where as if you passed in a .txt it would open in Notepad.

blak3r
  • 16,066
  • 16
  • 78
  • 98