Open a pdf with default windows behaviour from a WPF application - wpf

I want to open a PDF with the default windows behaviour the user has saved (e.g. internet explorer, adobe, whatever).
I found this solution
Opening a .pdf file in windows form through a button click
and implemented it here:
ProcessStartInfo startInfo = new ProcessStartInfo("MyPdfPath");
Process.Start(startInfo);
Sadly I got an Error:
System.ComponentModel.Win32Exception: "The specified executable is not a valid application for this OS platform."
I tried to google this error, but nothing of the first ten solution ideas worked.

The system is treating it like an executable, one way to get the document behavior is to set UseShellExecute to true:
ProcessStartInfo startInfo = new ProcessStartInfo("MyPdfPath");
startInfo.UseShellExecute = true;
Process.Start(startInfo);

Related

Application showing an exception"file not found "

I created an application in VS2010 with operating system WINDOWS XP.
Now, I updated the os to WIN 7 and also updated the location of the application.
So, while running the application for opening a file using open dialog box it showing some exception like"File Not Found".
It was working fine with WIN XP, but now it showing this error, if we keep that perticular file in bin folder its working fine , but if we choose a file from other drive or a folder it showing error.
enter code here
string chosen_file = "";
ofd.Title = "Open a file";
ofd.FileName = "";
ofd.Filter = "Text Files(*.txt)|*.txt|Rich Text Box(*.rtb)|*.rtb|Word Document(*.doc)|*.doc|HTML Pages(*.htm)|*.html|Cascading Style Sheet(*.css)|*.css|JAVA(*.java)|*.java|video file(*.wmv)|*.wmv|All Files(*.*)|*.*";
if (ofd.ShowDialog() == DialogResult.OK)
{
chosen_file = ofd.FileName;
// richTextBox2.LoadFile(chosen_file, RichTextBoxStreamType.PlainText);
var fileInfo = new FileInfo(ofd.FileName);
fileInfo.Length.ToString();
byte[] buffer = new byte[fileInfo.Length];
int length = (int)fileInfo.Length;
FileStream fileStream = new FileStream(fileInfo.Name, FileMode.Open, FileAccess.Read);
fileStream.Read(buffer, 0, length);}
My guess is you referenced a hard-coded path, probably to your "Documents" folder, rather than using an environment variable. With the change from XP to 7, that directory changed. I can't recall what the directory was for Windows XP, but it's now /users/username for Windows 7. Either way, you would be better off using an environment variable.
Look through your program for Documents and Settings and see if you can find it. If you go to a command prompt and type set, it should give you a list of environment variables you could use in place of whatever you were using.

WPF webrowser control - flash working only first time

I have a very strange problem on my wpf application.
I use a webbrowser control to display a third-party web page that uses flash to provide some functionality.
On internet explorer 9 and 10 i have a particular behaviour : the first time I open the webbrowser control everything works fine, and the application puts a .SWF file inside my temporary internet files folder. When I close and reopen the webcontrol, the following error arise :
"object does not support property or method 'UpdateTimeLeft'".
If i manually delete the .SWF file inside the temporary folder, close ad reopen the application, everything works fine again.
This is the code that arise the exception :
var swfUrl = "//" + serverName + "/js/tv/FlashTurboLotto3.swf";
var expressSwfUrl = "//" + serverName + "/js/tv/expressInstall.swf";
var flashvars = {};
var params = {wmode:"transparent", allowscriptaccess:"always" };
var attributes = {id:"FlashTurboLotto3", name:"FlashTurboLotto3", align:"top"};
swfobject.embedSWF(
swfUrl,
"flashcontent",
"100%",
"197",
"9.0.0",
expressSwfUrl,
flashvars,
params,
attributes
);
function updateTimeleft(drawID, timeleft)
{
var swf = document.getElementById("FlashTurboLotto3");
swf.updateTimeleft(parseInt(drawID), timeleft);
}
does anyone have an idea of why this appens and how to solve the issue?
Thank in advance.
I found the solution - It is a very particular bug of Flash. Sometimes happens that when the flash application runs in a non-native browser (like, in my case, a webbrowser control in WPF) the javascript fails at retrieving the .swf file from the cache, so the Flash object will not get initialized.
I solved, for now, by forcing to not use the browser cache, simply adding a random value to the .Swf download Url.

How to disable file in use check in WPF OpenFileDialog?

My WPF app is using the Microsoft.Win32.OpenFileDialog to select a SQL Server 2008 database to open.
It works OK but for one issue: When the database selected in the dialog was previously opened at some time since last boot, the file seems to be held open by SQL server in the background (even when it is not opened by my app and my app has been restarted). This causes a "file is used by another application" warning when OK is clicked in the OpenFileDialog and i can not use the dialog to open that particular database until the computer is rebooted. It seems the OpenFileDialog tries to open the file selected and doing that discovers that it is already opened by another app (SQL Server). How do i disable the OpenFileDialog from trying to open the selected file and just return the filename of the selected file without any checks?
My code looks like this:
public void OpenDatabase() {
// Let user select database to open from file browser dialog
// Configure open file dialog box
var dlg = new Microsoft.Win32.OpenFileDialog();
dlg.FileName = ""; // Default file name
dlg.DefaultExt = ".mdf"; // Default file extension
dlg.Filter = "Databases (.mdf)|*.mdf|All Files|*.*"; // Filter files by extension
dlg.CheckFileExists = false;
dlg.CheckPathExists = false;
// Show open file dialog box
bool? result = dlg.ShowDialog(); // Gives file in use warning second time!
// Process open file dialog box results
if (result == true) {
// Open document
string filename = dlg.FileName;
TryOpenDatabase(filename);
}
}
The underlying option is OFN_NOVALIDATE for early Windows versions, FOS_NOVALIDATE for the Vista dialog you get on later versions of Windows and .NET. The description from MSDN:
Do not check for situations that would prevent an application from opening the selected file, such as sharing violations or access denied errors.
Which is what you see happening now, the dialog sees a sharing violation on the database file. This option is in fact exposed on the OpenFileDialog wrapper class, add this line of code to fix your problem:
dlg.ValidateNames = false;
The MSDN forum has a post about this
It is in the OpenFileDialog API, you can turn that off using
ValidateNames = false
on the Dialog.

Adobe Flex OpenWithDefaultApplication Movie

I am working on an Adobe Air application that I would like to be able to launch a movie using the default application. The code I am trying is:
var file:File = new File;
//currentMovie.ConfiguredPath = 'D:\\Movies\\TestMovie.avi';
file.nativePath = currentMovie.ConfiguredPath;
file.openWithDefaultApplication();
I have also tried this:
//currentMovie.ConfiguredPath = file://D:/Movies/TestMovie.avi'
navigateToURL(newURLRequest(currentMovie.ConfiguredPath));
The first option does nothing. No application opens, no errors, nothing. The second option worked but it launches a separate window, downloads the file to the local user's download directory then opens the file (as long as you click "Open"). This is not exactly the behavior that I was hoping for.

how to toggle between the running exe files using c#.net code on button click?

sir i want to know that how could i navigate/toggle between the different exe files using c#.net code on my .net application on the button click event..basically i want to do toggling between the current and previously opened running exe files..im able to run an exe file on my .net application but not able to provide an option for opening the previously running exe file...on the whole i want to provide a facility same as we are having in the OS windows that we could toggle between the applications on the taskbar menu
how could i do the same in the c#.net application using c# code.
thanks
Although not sure if I got you correctly but if you want to invoke a new exe on any event use:-
System.Diagnostics.ProcessStartInfo f = new System.Diagnostics.ProcessStartInfo("C:\\windows\\system32\\rundll32.exe",
"C:\\windows\\system32\\shimgvw.dll,ImageView_Fullscreen " +
fileName.TrimEnd (null));
try
{
// Pass the ProcessStartInfo object to the Start function.
System.Diagnostics.Process.Start (f);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine (ex.ToString ());
}

Resources