Opening File with unknown Extension

A

Abhi

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.

Thanks.
Abhijeet Kumar.
 
A

Aneesh P

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.

Thanks.
Abhijeet Kumar.

I would suggest using rundll32.exe to invoke the shell32.dll with
OpenAs_RunDLL parameter.

Try this:

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

This woudl give you the Caution window displaying Openwith and Cancel
buttons.
 
A

Aneesh P

I would suggest using rundll32.exe to invoke the shell32.dll with
OpenAs_RunDLL parameter.

Try this:

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

This woudl give you the Caution window displaying Openwith and Cancel
buttons.

Am assuming windows has been installed to C:\. Change the path
according to your installation drive in code or retrieve the windows
path using environment variables - %WINDIR% or %SYSTEMROOT%.
 
A

Abhi

I would suggest using rundll32.exe to invoke the shell32.dll with
OpenAs_RunDLL parameter.

Try this:

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

This woudl give you the Caution window displaying Openwith and Cancel
buttons.

I tried the code given by you, it is giving me error as

---------------------------
Application Error
---------------------------
An error occurred please contact the adminstrator with the following
information:

No application is associated with the specified file for this
operation

Stack Trace:
at
System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo
startInfo)

at System.Diagnostics.Process.Start()

at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)

at Wachovia.CIB.CDF.Shell.frmMain.newHelpMenuItem_Click(Object
sender, EventArgs e) in c:\projects\vcs\vss\onesource 1.2 plus\client
\src\client\wachovia.cib.cdf.shell\frmmain.cs:line 3501

at
Syncfusion.Windows.Forms.Tools.XPMenus.BarItem.OnItemClicked(EventArgs
args)

at Syncfusion.Windows.Forms.Tools.XPMenus.BarItem.PerformClick()

at
Syncfusion.Windows.Forms.Tools.XPMenus.MenuGrid.NotifyItem(BarItem
item)

at
Syncfusion.Windows.Forms.Tools.XPMenus.MenuGrid.HidePopup(PopupCloseType
popupCloseType)

at
Syncfusion.Windows.Forms.Tools.XPMenus.MenuGrid.ProcessItemClick(BarItem
item)

at
Syncfusion.Windows.Forms.Tools.XPMenus.MenuGrid.OnMouseUp(MouseEventArgs
e)

at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)

at System.Windows.Forms.Control.WndProc(Message& m)

at Syncfusion.Windows.Forms.ScrollControl.WmMouseUp(Message& msg)

at Syncfusion.Windows.Forms.ScrollControl.WndProc(Message& msg)

at Syncfusion.Windows.Forms.Grid.GridControlBase.WndProc(Message&
msg)

at Syncfusion.Windows.Forms.Tools.XPMenus.MenuGrid.WndProc(Message&
m)

at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)

at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)

at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32
msg, IntPtr wparam, IntPtr lparam)
---------------------------
Abort Retry Ignore
---------------------------


What is OpenAs_RunDLL .dat. I am trying to open a .bak File.

regards,
Abhijeet Kumar.
 
A

Abhi

I would suggest using rundll32.exe to invoke the shell32.dll with
OpenAs_RunDLL parameter.

Try this:

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

This woudl give you the Caution window displaying Openwith and Cancel
buttons.

Hi,
I restarted the System and now the error message has changed to
 
J

Jon Skeet [C# MVP]

I restarted the System and now the error message has changed to

Did you actually cut and past Aneesh's code? He has a space between
RunDLL and .dat - the error message you're getting *suggests* that your
code doesn't.
 
A

Aneesh Pulukkul

Did you actually cut and past Aneesh's code? He has a space between
RunDLL and .dat - the error message you're getting *suggests* that your
code doesn't.

Exactly..while copying some formatting is lost I guess. There should
be a space between OpenAs_RunDLL and .dat which I can't see in the
error message.
 
A

Abhi

Exactly..while copying some formatting is lost I guess. There should
be a space between OpenAs_RunDLL and .dat which I can't see in the
error message.- Hide quoted text -

- Show quoted text -

That was the mistake, Its working now,

thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top