Error Printing: No application is associated with the specified file for this operation - vb.net

I have a VB WinForms app that prints PDFs using Process.Start, and it has been working fine for ages on Windows 10. Today I go to use it and get the following error message:
"No application is associated with the specified file for this operation"
Nothing has changed to cause this, I have not changed PDF Viewer or uninstalled anything. I can open a PDF by double clicking on it no problems.
I wrote a small console app to replicate the issue and prove this had nothing to do with the WinForms app:
Sub Main()
Dim pi As New Diagnostics.ProcessStartInfo
Dim url As String = "C:\PathToPDF\.pdf"
pi.FileName = url
pi.Verb = "PrintTo"
pi.CreateNoWindow = True
pi.Arguments = """Microsoft Print to PDF"""
pi.UseShellExecute = True
Console.WriteLine(url)
Diagnostics.Process.Start(pi)
Console.ReadKey()
End Sub
The above program replicates the error. I have verified that the file exists and is accessible and tried it with double slashes and single slashes, they give the same error:
"No application is associated with the specified file for this operation".
The same application is working fine on other PCs in the office.
Has anyone else had this happen, and if so how did they fix it?
My PDF viewer is PDFXChange Viewer, and it has been working fine PDFs printed using this method for years. I have not updated the program, and it says in the about page that the last installed updated was in 2016.
Regards.

If I am right, I had the same issue. Maybe you can try it in a slightly other way. I have this in my application and it works fine:
Dim myp As New Process
myp.StartInfo.FileName = filename 'Full path to pdf
myp.Start()

Related

vb.net external dos command hangs without returning output

I am running a dos command from my app, however the dos window just hangs without returning any output. It does work under Windows 8, but not Windows 7.
This is my code:
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim process As New Process()
process.StartInfo.FileName = "C:\Program Files (x86)\Pico Technology\PicoScope6\picoscope.com"
process.StartInfo.UseShellExecute = False
process.StartInfo.Arguments = "/a Measurements.CSV?"
process.StartInfo.RedirectStandardInput = True
process.StartInfo.RedirectStandardOutput = True
process.Start()
Dim reader As StreamReader = process.StandardOutput
Dim output As String = reader.ReadToEnd()
RichTextBox1.Text = output
End Sub
picoscope.com is a 'wrapper' supplied by the PicoScope vendor - it appears to be a very small .net app, which calls the main PicoScope.exe program with some commands (unknown). The PicoScope.exe app needs to be running for this to work.
I wrote a small console app, and this works just fine - however I can't even call my console app from a forms based application.
Code for console app:
Sub Main()
Dim process As New Process()
process.StartInfo.FileName = "C:\Program Files (x86)\Pico Technology\PicoScope6\picoscope.com"
process.StartInfo.Arguments = "/a Measurements.CSV?"
process.StartInfo.UseShellExecute = False
process.StartInfo.RedirectStandardOutput = True
process.Start()
' Synchronously read the standard output of the spawned process.
Dim reader As StreamReader = process.StandardOutput
Dim output As String = reader.ReadToEnd()
process.WaitForExit()
process.Close()
End Sub
This seems to be some strange issue with Windows 7.
I can open cmd.exe without any issue, as well as spawn other applications.
It's almost as if there is some conflict between the running picoscope programs and my application.
Attempting to debug doesn't seem to help at all, with the exception of this:
A first chance exception of type 'System.InvalidOperationException' occurred in System.dll
I have tried several different approaches, disabling shell redirection, even trying to redirect the output to a file. But it would appear that anything to do with this Picoscope app just prevents it working.
I even tried writing another console app to redirect text output to the console and to a file and they worked (because the picoscope app wasn't involved!).
Likewise, a simple dos batch file with "echo Hello World" in it is successfully redirected to my forms app - so I'm sure my code is correct.
I am now at a loss as to what the issue could be here.
I'm using Visual Studio 2013, Windows 7 on SP1.
Can anyone offer any suggestions?
Apologies for the long post, I was trying to give as much detail as possible.
EDIT: Even more curious is that I tried my working app on another Windows 7 PC and it works...
I suspect this must be related to my specific Windows 7 PC (a fresh build).
I compared UAC settings with the other PC - they are the same.
Thanks.

Access to path is denied when trying to import from the client's desktop with SSIS

I'm creating a html page that will import an excel file in to a tracking system. On a button click event excel file is located / ssis package is fired / data imported then closed out. Thats the idea work flow. Problem is the excel file access is being denied before the package even executes
Here is the exact error :
I've tried :
excel file properties have been shared to everyone
identity impersonate set to true
hard coding the path
here is the VB code
Protected Sub bntExecute_Click(sender As Object, e As EventArgs) Handles btnExecute.Click
Dim app As Application = New Application()
Dim package As Package = Nothing
'Dim fileName As String = "C:\Users\Desktop\T. Bryant III\PTSID_Update_Template"'
Try
Dim fileName As String = Server.MapPath(System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName.ToString()))
FileUpload1.PostedFile.SaveAs(fileName)
package = app.LoadPackage("#C:\Users\Desktop\T.Bryant III\KitImport", Nothing)
'excel connection from package'
package.Connections("SourceConnectionExcel").ConnectionString = "provider=Microsoft.Jet.OLEDB.4.0data source =" + fileName + "Extended Properties = Excel 8.0"
'Execute the pakage'
Dim results As Microsoft.SqlServer.Dts.Runtime.DTSExecResult = package.Execute()
Catch ex As Exception
Throw ex
Finally
package.Dispose()
package = Nothing
End Try
End Sub
Thanks in advance or if there is an easier way to do this please let me know. The package when executing it in ssis works fine with its own connection manager etc.
A few things to try. If they don't work for you as permanent solutions, they should at least confirm that your code is working and you are dealing with a persmissions issue (which appears to be the case).
Move your file to the public folder (C:\Users\Public).
Run your application (or web browser) as an administrator (if applicable to your version of Windows).
If you are using a web browser, try using a different one.
If nothing else works, try pasting your code into a Windows Form Application.
If you still get the same error after trying all of this, it's time to take another look at your code. Remove the Try/Catch block to determine precisely which line is throwing the error. If you've tried hard coding, I'm guessing it's the SaveAs method. I'm not sure what class FileUpload1 is, but some SaveAs methods won't overwrite existing files unless you explicitly tell them to. Check the appropriate documentation and see if you don't need to pass a True value somewhere along with filename.
Update us with the results. At the very least, this should narrow down your problem and allow for a better diagnosis of it.

No application is associated with the specified file for this operation (VB.NET)

We have a Win Forms application that produces a pdf with iTextSharp, saves it to a local directory and the application then opens the file. With one customer (all XP boxes and Adobe Reader 11) it throws the following error
No application is associated with the specified file for this operation
at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start()
Which would suggest Adobe Reader is not associated correctly with the pdf extension apart from the fact they can navigate to the local directory and open the file without any problem whatsoever.
Anyone come across this oddity before?
Edit re ZippyV - example of a typical sub
Public Sub PDF_Functions_LogCalls_RunReport(ByVal Customer As Boolean)
Try
Dim vOutput As String = LogCalls_Run(Customer)
If Left(vOutput, 5) = "Error" Then
TaskDialog.Show(MainForm, AppBoxError("File Error", vOutput, "Error"))
Exit Sub
End If
If System.IO.File.Exists(vOutput) Then
Dim P As New Process
P.StartInfo.FileName = vOutput
P.StartInfo.Verb = "Open"
P.Start()
End If
Catch ex As Exception
EmailError(ex)
Exit Sub
End Try
End Sub
You're reading the error message wrong. I've added emphasis to the relevant part:
No application is associated with the specified file for this operation
This means that there is no application associated with the verb "Open". Change your code to simply use an empty string (or just don't set) the Verb:
P.StartInfo.FileName = vOutput
P.StartInfo.Verb = ""
P.Start()
This uses whatever the default operation is for the .pdf format, which will match the operation the user would get if they double-clicked the file in Windows Explorer.
Recent versions of Acrobat are setting the default action to "Open with Adobe Reader XI" instead of just "Open", as you can see if you right-click a .pdf file.
This is seemingly what's causing the "not associated for this operation" error.
This error actually happens when there is a difference between the default behaviour of opening the file and the relative behaviour of opening the file.
For example, if you have selected the default application to open .pdf files as Internet Explorer, and you are trying to open the same file using Process.Start() method. You will receive an exception because as per the default operations it should open that file in Internet Explorer and your application is trying to open it using Adobe reader.
To rectify this set the default application for the .pdf file as Adobe Reader and you won't receive this error any more.

Opening a file using impersonation

I have been searching the web looking for a way to open a WORD file from a secure network folder by impersonating a user who has access. The closest I've come to finding the answer was this from 2 years ago:
Impersonating in .net (C#) & opening a file via Process.start
Here is the code that I am using. When I set the arguments = LocalFile_Test, everything works perfectly because the user is accessing the local c:\ that is has access to. But when I set arguments = RemoteFile_Test, Word opens up a blank document which is the same effect as if I put garbage in the arguments. So it appears that it cannot find the file even though when I login with the user/domain/password that I specify in the properties below, I can find that exact file name and it is not empty. Does anything jump out at you right away? I appreciate your time.
Dim LocalFile_Test As String = "C:\New.docx"
Dim RemoteFile_Test As String = "\\Server1\Apps\File\New.docx"
Dim MyStartInfo As New System.Diagnostics.ProcessStartInfo
MyStartInfo.FileName = "C:\Program Files\Microsoft Office\Office12\WINWORD.exe "
MyStartInfo.Arguments = LocalFile_Test
MyStartInfo.LoadUserProfile = True
MyStartInfo.UseShellExecute = False
MyStartInfo.UserName = "specialuser"
MyStartInfo.Domain = "mydomainname"
MyStartInfo.Password = New System.Security.SecureString()
MyStartInfo.Password.AppendChar("p"c)
MyStartInfo.Password.AppendChar("a"c)
MyStartInfo.Password.AppendChar("s"c)
MyStartInfo.Password.AppendChar("s"c)
Process.Start(MyStartInfo)
My understanding is that you are trying to get a password protected file from a server, and when you do process start, it just opens up a blank word doc. I think the error is how you are trying to get the file, I think you have to map the actual physical path of the file on the server, like
System.Web.HttpContext.Current.Server.MapPath("\\Server1\Apps\File\New.docx")
From there, I am fairly certain, you need to create network credentials for the user like
System.Net.NetworkCredential=New NetworkCredential(userName:=, password:=)
Finally, once that is done, you can either write the file, or transmit the file like so...
System.Web.HttpContext.Current.Response.TransmitFile(file name)
System.Web.HttpContext.Current.Response.WriteFile(file name)
Then,once you get the file, you can try to open it with process start.
Hope that helps, let me know if what I said doesn't work.

Word.Interop Print to pcl file.

I'm working on an application which automatically generates faxes.
The system uses word interop to fill a template .doc file with values (which works fine), but when it comes to be time to print the resulting file to pcl, I'm having issues.
So, we've got a HP Laserjet printer set up which prints .pcl files.
Dim appWord As New Word.Application
Dim doc As New Word.Document
appWord.ActivePrinter = PCL_PRINTER
doc = appWord.Documents.Open(APPLICATION_DIR & "LTL_" & n.Language & ".doc")
... (Fill the file with values)
outFile = APPLICATION_DIR & "Faxout\DROPDIR\" & n.Order & ".pcl"
doc.PrintOut(True, True, , outFile, , , , , , , True)
'cleanup...
Problem is that when the doc.PrintOut line runs, word complains, popping up a message:
I've tried all sorts of combinations of things, but the error message persists. Very occasionally, the thing actually works and generates a usable pcl file, but 98% of the time, the error message pops up and the whole process grinds to a halt.
We have other applications on the same server which do almost exactly the same thing, except they print from excel instead of word, and they work without throwing the error.
Can anyone help?
Don't know if this helps, but do you have an antivirus software on that machine? If so, did you try to disable it? There are sometimes issues with virus scanners accessing files in background, blocking access or deletion of temporary files.
I just figured it out. Apparently you have to create the file before you attempt to write to it.
eg:
Dim fs As FileStream
fs = System.IO.File.Create(outFile)
fs.Close()
fs = Nothing
doc.PrintOut(True, True, , outFile, , , , , , , True)
I'd tried creating the file earlier, but didn't realize that Create returned a filestream which needed to be closed before word would print out to it.
Is there any way to create a file in vb.net that doesn't create a lock on a file with a filestream object? This version works, but it'd be nice to clean up the code a bit.