Open a file from a remote network share - vb.net

I am trying to open a file from a server
I currently have
Dim attachedFilePath As String = "\\myserver\myshare\test.txt"
File.Open(attachedFilePath, FileMode.Open, FileAccess.Read, FileShare.Read)
This does not open a file.
However, if I change the path to be local then there is no issue.
Dim attachedFilePath As String = "c:\...\test.txt"
So, is there a way to open a file from remote storage?

File.Open is for reading the contents of a file. Use Process.Start to launch the default application for that file type
Dim Path = "\\myserver\myshare\test.txt"
System.Diagnostics.Process.Start(Path)

Related

How to upload file using Geckofx 60 in VB.NET without opening dialog?

I'm using Geckofx 60 to fill a web form. I have to attach an image file also. I can select/click Chose file and Select a file manually. Is there any way to select a local file without opening dialog?
Dim el As GeckoHtmlElement = GeckoWebBrowser1.DomDocument.GetElementsByTagName("input").FirstOrDefault(Function(elz) elz.GetAttribute("type") = "file")
Dim fileNames = New IntPtr(0) {}
Dim domInput = Xpcom.QueryInterface(Of nsIDOMHTMLInputElement)(el.DOMHtmlElement)
domInput.MozSetFileNameArray(fileNames, CUInt(fileNames.Length)) 'Getting Error
Marshal.ReleaseComObject(domInput)
Dim ev As DomEventArgs = GeckoWebBrowser1.Document.CreateEvent("HTMLEvents")
Dim webEvent = New [Event](GeckoWebBrowser1.Window.DomWindow, TryCast(ev.DomEvent, nsISupports))
webEvent.InitEvent("change", True, True)
el.GetEventTarget().DispatchEvent(ev)
CustomMarshalers.WStringMarshaler().CleanUpNativeData(fileNames)
I have Got the Error above after checking How to choose and upload a local file to a website using Geckofx in C#?

OpenFileDialog set complete path

Dim streamf As System.IO.Stream
Dim stringf As String
stringf = OpenFileDialog1.FileName.ToString()
streamf = OpenFileDialog1.OpenFile()
I have a file ScreenShot.png attached on the application installation path which means if user run the .exe file from Desktop it will store ScreenShot.png on his desktop and so on.
Now i need to get the full path to that ScreenShot.png file to be sent in the FAX
Dim srcBt As Byte()
Dim encodedBase64 As String
Dim filereader As New IO.FileStream(stringf, IO.FileMode.Open)
ReDim srcBt(filereader.Length)
filereader.Read(srcBt, 0, filereader.Length)
filereader.Close()
encodedBase64 = System.Convert.ToBase64String(srcBt)
So the problem part is how can i put that location of the file to the Stringf ? Because example i have force user to locate the file using OpenFileDialog and i have predefined file
This should convert any (relative) path to an absolut path:
Dim stringf_info As New System.IO.FileInfo(stringf)
stringf = stringf_info.FullName

Write File with text from resources visual basic

I'm writing some program in VB, and I want to create txt file with text from file in resources. You didn't understood, did you? So, it goes like this.
Dim path As String = "c:\temp\MyTest.txt"
' Create or overwrite the file.
Dim fs As FileStream = File.Create(path)
' Add text to the file.
Dim info As Byte() = New UTF8Encoding(True).GetBytes("This is some text in the file.")
fs.Write(info, 0, info.Length)
fs.Close()
is code for creating txt file with certain text. But, I need the following.
Dim fd As New FolderBrowserDialog
fd.ShowDialog()
is the only function that I have in program, and, when folder is selected, I need to create file in that folder, file's name should be config.cfg, but, text in file which is going to be created in selected folder should be text from mine txt file which is in Recources.
I've tried
Dim path As String = fd.SelectedPath
Dim fs As FileStream = File.Create(path)
' upisuje tekst u fajl
Dim info As Byte() = New UTF8Encoding(True).GetBytes(application.startuppath & "\..\..\Resources\config.cfg")
fs.Write(info, 0, info.Length)
fs.Close()
but the text I got in file is directory from where is my program debugged.
Any ideas to do this? :)
If you added a text file to your resources, then you can try something like this:
Using fbd As New FolderBrowserDialog
If fbd.ShowDialog(Me) = DialogResult.OK Then
File.WriteAllText(Path.Combine(fbd.SelectedPath, "config.cfg"), My.Resources.config)
End If
End Using
The file I added was called config, and it made a config.txt file in my resource library.

Epplus Csharp | How to manipulate already opened(in use) Excel File

Epplus Csharp | How to manipulate already opened(in use) Excel File. it works only when Excel file is closed. I can do like closing before code execution and reopen again, but do not think it's a way to go as my file contains more than 50000 rows (file size is big). Please advise, how to figure it out.
Thanks in advance
Rather than using a FileInfo to open the ExcelPackage workbook, use a stream:
FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
ExcelPackage pkg = new ExcelPackage(fs);
path is the path to the file that you would otherwise use for FileInfo
FileMode says you want to open it
FileAccess says you want to read it
FileShare says that other programs can read/write.
With the workbook specified by path open in excel, you will be able to read lines from the file, but will get an exception if you try to call ExcelPackage.Save().
It is only possible, if the file is opened for reading by a third-party, but not for writing.
If the third-party opened the file for writing, your attempt to open the same file even just for reading will result in System.IO.IOException. In fact, it's all about FileStream and not specific to EpPlus and ExcelPackage. Example in terms of .NET:
var fileStream1 = new FileStream(#"File.ext", FileMode.Open, FileAccess.Read);
var fileStream2 = new FileStream(#"File.ext", FileMode.Open, FileAccess.Read);
will work just fine. But the second line of the following fragment:
var fileStream1 = new FileStream(#"File.ext", FileMode.Open, FileAccess.Write);
var fileStream2 = new FileStream(#"File.ext", FileMode.Open, FileAccess.Read);
will result in a System.IO.IOException.

Extracting from your resources VB.net

I have a .zip folder in the .exe resources and I have to move it out and then extract it to a folder. Currently I am moving the .zip out with System.IO.File.WriteAllByte and unziping it. Is there anyway to unzip straight from the resources to a folder?
Me.Cursor = Cursors.WaitCursor
'Makes the program look like it's loading.
Dim FileName As FileInfo
Dim Dir_ExtractPath As String = Me.tb_Location.Text
'This is where the FTB folders are located on the drive.
If Not System.IO.Directory.Exists("C:\Temp") Then
System.IO.Directory.CreateDirectory("C:\Temp")
End If
'Make sure there is a temp folder.
Dim Dir_Temp As String = "C:\Temp\Unleashed.zip"
'This is where the .zip file is moved to.
Dim Dir_FTBTemp As String = Dir_ExtractPath & "\updatetemp"
'This is where the .zip is extracted to.
System.IO.File.WriteAllBytes(Dir_Temp, My.Resources.Unleashed)
'This moves the .zip file from the resorces to the Temp file.
Dim UnleashedZip As ZipEntry
Using Zip As ZipFile = ZipFile.Read(Dir_Temp)
For Each UnleashedZip In Zip
UnleashedZip.Extract(Dir_FTBTemp, ExtractExistingFileAction.DoNotOverwrite)
Next
End Using
'Extracts the .zip to the temp folder.
So if you're using the Ionic library already, you could pull out your zip file resource as a stream, and plug that stream into Ionic to decompress it. Given a resource of My.Resources.Unleashed, you have two options for getting your zip file into a stream. You can load up a new MemoryStream from the bytes of the resource:
Using zipFileStream As MemoryStream = New MemoryStream(My.Resources.Unleashed)
...
End Using
Or you can use the string representation of the name of the resource to pull a stream directly from the assembly:
Dim a As Assembly = Assembly.GetExecutingAssembly()
Using zipFileStream As Stream = a.GetManifestResourceStream("My.Resources.Unleashed")
...
End Using
Assuming you want to extract all the files to the current working directory once you have your stream then you'd do something like this:
Using zip As ZipFile = ZipFile.Read(zipFileStream)
ForEach entry As ZipEntry In zip
entry.Extract();
Next
End Using
Taking pieces from here and there, this works with 3.5 Framework on Windows 7:
Dim shObj As Object = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"))
Dim tmpZip As String = My.Application.Info.DirectoryPath & "\tmpzip.zip"
Using zip As Stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("myProject.myfile.zip")
Dim by(zip.Length) As Byte
zip.Read(by, 0, zip.Length)
My.Computer.FileSystem.WriteAllBytes(tmpZip, by, False)
End Using
'Declare the output folder
Dim output As Object = shObj.NameSpace(("C:\destination"))
'Declare the input zip file saved above
Dim input As Object = shObj.NameSpace((tmpZip)) 'I don't know why it needs to have double parentheses, but it fails without them
output.CopyHere((input.Items), 4)
IO.File.Delete(tmpZip)
shObj = Nothing
Sources: answers here and https://www.codeproject.com/Tips/257193/Easily-Zip-Unzip-Files-using-Windows-Shell
Since we are using the shell to copy the files, it will ask the user to overwrite them if already exist.