Writing PDF in C:\ folder - vb.net

I'm using .NET 4 on Windows 10.
I have a winform application written in vb.net which converts TIFF into PDF using PDFSharp api.
When I try to save the PDF into the C:\ folder there is no exception raised but nothing is written.
When I check if I have write access permission on folder C:\ , VB.NET tells me I do.
I'm using this chunk of code:
Private Function HasFolderWriteAccess(path As String) As Boolean
Try
Using inputstreamreader As New StreamReader(path)
inputstreamreader.Close()
End Using
Using inputStream As FileStream = File.Open(path, FileMode.Open, FileAccess.Write, FileShare.None)
inputStream.Close()
Return True
End Using
Catch ex As Exception
Return False
End Try
End Function
When I try to write a file using a StreamWriter, I face the same behavior:
Dim FILE_NAME As String = "C:\test2.txt"
Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
objWriter.Write("Some text....")
objWriter.Close()
MessageBox.Show("Text written to file")
Is there some kind of magic happening in the Windows 10 C:\ folder that I don't know ?
Thanks for your replies.

Chris Dunaway's answer (in the comments) is right:
...look in this folder instead: C:\Users\User_name\‌​AppData\Local\Virtual‌​Store and see if your file is there. Windows doesn't allow writing of files to certain folders and silently redirects them to the virtual store...

Related

vb.net resaving binary files

I have the following method to save Custom As List of (CustomItem)" to a binary file:
Dim st As FileStream
Try
If Not Directory.Exists(Path.GetDirectoryName(FilePath)) Then Directory.CreateDirectory(Path.GetDirectoryName(FilePath))
st = File.Open(FilePath, FileMode.OpenOrCreate)
Dim SerialObj As New BinaryFormatter()
SerialObj.Serialize(st, Custom)
st.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
This doing great and I can read the content but after changing a string property in the "CustomItem" class and saving the file again it saves the file without any issues. But the issue appears when I read the file again it gets that the file has zero "CustomItem"(s) but it has at least 3 "CustomItem"s inside the file.
Sorry for lengthiness,
but what can be the issue?

How to download file from SFTP in vb.net

I am trying to use the classes in Renci.SshNet.Sftp to download a file from an SFTP server with VB.NET. Here is my code:
Using client As New SftpClient("server", "test", "test")
client.Connect()
Dim list As List(Of SftpFile) = CType(client.ListDirectory(""), List(Of SftpFile))
'------------------------
For Each sFile As SftpFile In list
Console.WriteLine(sFile.Name)
client.DownloadFile("path", ????)
Next
client.Disconnect()
End Using
With this code I can connect to the server and see the file, but I can't download it. I don't know how to call the DownloadFile method.
The second parameter of the DownloadFile method takes a stream. So, you just need to create a new FileStream to write the downloaded data to a new file, like this:
Using fs As New FileStream(localFilePath, FileMode.CreateNew, FileAccess.Write)
client.DownloadFile(serverFilePath, fs)
End Using

Check the file whether is it by another process

I have some ask to you to check and advice if my code is enough and correct. My application works on files sometimes its downloading multiple files by windows service and then by other windows service parser process those files are parsing. Unfortunately before process file has to be finished downloading before process parser should open the file and play with it therefore i prepared some code to check firstly whether the file is free - means not open by another process or some person, whatever. I use most cases streamreader to open the file and play with it but as i said i would like firstly check file if its ready.
This is my code, what do you think:
Public Shared Function FileInUse(ByVal sFile As String, ByVal log As String) As Boolean
Dim thisFileInUse As Boolean = True
Dim counter As Integer = 0
For i = 0 To 30
counter += 1
If System.IO.File.Exists(sFile) Then
Try
Using f As New IO.FileStream(sFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
thisFileInUse = False
Exit For
End Using
Catch
System.Threading.Thread.Sleep(10000) '10 sec
End Try
End If
Next
Return thisFileInUse
End Function

how to open selected files through the default application of the file in VB 2010?

I have Windows application written in VB 2010.Here, user may select any file from open dialog.So, I want to open the file in corresponding application.for example, suppose user selecting docx file then i have to open the file using msword,suppose,if it is a pdf file, then i have to open with adobe reader or available pdf reader(default application).
Is this possible to do?
Shell and the Windows API CreateProcess() are for starting executable files.
If you're loading a document/file then these are handled by ShellExecute() and can be initiated in .NET using the Process.UseShellExecute property:
Private Function ShellExecute(ByVal File As String) As Boolean
Dim myProcess As New Process
myProcess.StartInfo.FileName = File
myProcess.StartInfo.UseShellExecute = True
myProcess.StartInfo.RedirectStandardOutput = False
myProcess.Start()
myProcess.Dispose()
End Function
Taken from the #VB wiki.
Try this:
now with openfiledialog
Dim OpenFileDlg as new OpenFileDialog.
OpenFileDlg.FileName = "" ' Default file name
OpenFileDlg.DefaultExt = ".xlsx" ' Default file extension
OpenFileDlg.Filter = "Excel Documents (*.XLSX)|*.XLSX"
OpenFileDlg.Multiselect = True
OpenFileDlg.RestoreDirectory = True
' Show open file dialog box
Dim result? As Boolean = OpenFileDlg.ShowDialog()
' Process open file dialog box results
for each path in OpenFileDlg.Filenames
Try
System.Diagnostics.Process.Start(Path)
Catch ex As Exception
MsgBox("Unable to load the file. Maybe it was deleted?")
End Try
If result = True Then
' Open document
Else
Exit Sub
End If
next
This will work if the file is registered with the OS. Use Try catch because it can throw errors if the file is in use.
Edit: It uses always the default application.

VB.NET 2008, Windows 7 and saving files

We have to learn VB.NET for the semester, my experience lies mainly with C# - not that this should make a difference to this particular problem.
I've used just about the most simple way to save a file using the .NET framework, but Windows 7 won't let me save the file anywhere (or anywhere that I have found yet). Here is the code I am using to save a text file.
Dim dialog As FolderBrowserDialog = New FolderBrowserDialog()
Dim saveLocation As String = dialog.SelectedPath
... Build up output string ...
Try
' Try to write the file.
My.Computer.FileSystem.WriteAllText(saveLocation, output, False)
Catch PermissionEx As UnauthorizedAccessException
' We do not have permissions to save in this folder.
MessageBox.Show("Do not have permissions to save file to the folder specified. Please try saving somewhere different.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Catch Ex As Exception
' Catch any exceptions that occured when trying to write the file.
MessageBox.Show("Writing the file was not successful.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
The problem is that this using this code throws an UnauthorizedAccessException no matter where I try to save the file. I've tried running the .exe file as administrator, and the IDE as administrator.
Is this just Windows 7 being overprotective? And if so, what can I do to solve this problem? The requirements state that I be able to save a file!
Thanks.
This code:
Dim dialog As FolderBrowserDialog = New FolderBrowserDialog()
Dim saveLocation As String = dialog.SelectedPath
Is giving you the location of a folder. Then you're trying to save a file with the same name as the folder. Instead, I assume you want to save a file inside that folder:
Dim saveLocation As String = dialog.SelectedPath
saveLocation = Path.Combine(saveLocation, "SomeFile.txt")
That will create a file called "SomeFile.txt" inside the selected folder.
Alternatively, instead of using FolderBrowserDialog to choose a folder, use SaveFileDialog to select the actual file instead.