Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C# Programming

Opening File with Unknown Extension C#


Abhijeet,

You are looking for the Windows Shell32.dll "ShellExecute" function.  Except
for the actual reference to the Windows API, which I don't know how to
convert from VB to C#, here is the code to open an arbitrary file.
Hopefully someone else can convert the API reference to C# for you.

// The Declare statement below needs to be converted to C#.  The converstion
will start
//  [DLLImport "SHEL32.DLL" ...

// Private Declare Function ShellExecute Lib "shell32.dll" Alias
"ShellExecuteA" _
//    (ByVal hwnd As IntPtr, _
//     ByVal lpOperation As String, _
//     ByVal lpFile As String, _
//     ByVal lpParameters As String, _
//     ByVal lpDirectory As String, _
//     ByVal nShowCmd As Int32) As Int32

enum WindowShowConstants {
    SHOW_FULLSCREEN = 3,
    SHOW_ICONWINDOW = 2,
    SHOW_OPENNOACTIVATE = 4,
    SHOW_OPENWINDOW = 1

}

Int32 OpenFile(string FileName)
{
    return OpenFile(FileName, WindowShowConstants.SHOW_OPENWINDOW);

}

Int32 OpenFile(string FileName, WindowShowConstants nShowCommand)
{
    return ShellExecute(IntPtr.Zero, "open", FileName, "", "",
(Int32)nShowCommand);

}

Mike.

"Abhi" <Abhijeet1@gmail.com> wrote in message

news:1180985399.349523.18090@w5g2000hsg.googlegroups.com...

> "Abhi" <Abhijeet1@gmail.com> wrote in message
> news:1180985399.349523.18090@w5g2000hsg.googlegroups.com...
> Hi,
> I want to open a File with unknown Extension using C#. What i want
> is
> i do System.Diagnostics.Process.Start("FileName");
> Now if File is associated with any program then the File with open
> with that Application, this is working fine.

> My requirement says, if file extension is unknown or not associated
> with any application then, it should open the Default windows Program
> association box and allow user to select a program to open that file.

Create a ProcessStartInfo object and set the ErrorDialogProperty to
true.  Ex:

System.Diagnostics.ProcessStartInfo psi = new
System.Diagnostics.ProcessStartInfo("FileName");
psi.UseShellExecute = true;
psi.ErrorDialog = true;
System.Diagnostics.Process.Start(psi);
--
Tom Porterfield

-----------------------------------------------Reply-----------------------------------------------

Tom,

Thanks - I had been looking for this for myself as well.  That's why my code
still used the ShellExecute API directly.

Mike Ober.

"Tom Porterfield" <tppor@mvps.org> wrote in message

news:%23o$ViX3pHHA.4152@TK2MSFTNGP04.phx.gbl...

On Jun 5, 7:11 pm, "Michael D. Ober" <obermd.@.alum.mit.edu.nospam>
wrote:

I guess I've already posted this code snippet - still:

try
            {
                ProcessStartInfo psi = new ProcessStartInfo(@"C:
\list.dat");
                Process.Start(psi);
            }
            catch (Win32Exception ex)
            {
                if (ex.ErrorCode == -2147467259)
                    //ErrorCode for No application is associated with
the specified file for
                    //this operation
                {
                    ProcessStartInfo psi = new ProcessStartInfo(@"C:
\WINDOWS\system32\rundll32.exe");
                    psi.Arguments = @" C:\WINDOWS
\system32\shell32.dll, OpenAs_RunDLL .dat";
                    Process.Start(psi);
                }
            }

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc