Why doesn't FTP request allow "%20"? - vb.net

I am making an FTP request and whenever the parameter remoteFilePath contains a string with "%20" I get an error that the file cannot be found. How can I get around this?
Dim remoteFileWriteTime As Date = Ftp.GetDateTimeStamp(remoteFilePath).ToLocalTime()
Note: I am not responsible for naming the files as it is not my server.

Try encoding the filename using this:
Dim baseUri as String = "ftp://SomeFtpServer/"
Dim file as String = "Strangely%20Named%20File.pdf"
Dim myUri as String = baseUri & HttpUtility.UrlEncode(file)
Dim remoteFileWriteTime As Date = Ftp.GetDateTimeStamp(myUri).ToLocalTime()
You might need to encode using Uri.EscapeDataString if HttpUtility isn't encoding it correctly. I'm not sure exactly which encoding scheme FTP expects offhand.

Related

VB.NET: Modifying non-text file as text without ruining it

I need my application to find and modify a text string in a .swp file (generated by VBA for SOLIDWORKS). If I open said file as text in Notepad++, most of the text looks like this (this is an excerpt):
Meaning there is readable text, and symbols that appear as NUL, BEL, EXT and so on, depending on selected encoding. If I make my changes via Notepad++ (finding and changing "1.38" to "1.39"), there are no issues, the file can be opened via SOLIDWORKS and is still recognized as valid. After all, I don't need to modify these non-readable bits. However, if I do the same modification in my VB.NET application,
Dim filePath As String = "D:\OneDrive\Desktop\launcher macro.swp"
Dim fileContents As String = My.Computer.FileSystem.ReadAllText(filePath, Encoding.UTF8).Replace("1.38", "1.39")
My.Computer.FileSystem.WriteAllText(filePath, fileContents, Encoding.UTF8)
then the file gets corrupted, and is no longer recognized by SOLIDWORKS. I suspect this is because ReadAllText and WriteAllText cannot handle whatever data is in these non-readable bits.
I tried many different encodings, but it seems to make no difference. I am not sure how Notepad++ does it, but I can't seem to get the same result in my VB.NET application.
Can someone advise?
Thanks to #jmcilhinney, this is a solution that worked for me - reading file as bytes, converting to string, and then saving, using ANSI formatting:
Dim file_name As String = "D:\OneDrive\Desktop\launcher macro.swp"
Dim fs As New FileStream(file_name, FileMode.Open)
Dim binary_reader As New BinaryReader(fs)
fs.Position = 0
Dim bytes() As Byte = binary_reader.ReadBytes(binary_reader.BaseStream.Length)
Dim fileContents As String = System.Text.Encoding.Default.GetString(bytes)
fileContents = fileContents.Replace("1.38", "1.39")
binary_reader.Close()
fs.Dispose()
System.IO.File.WriteAllText(file_name, fileContents, Encoding.Default)

How to recombine document pages stored as separate Base64 strings in FileNet using VB.Net?

I have a document that is stored on FileNet. Each page of the document is stored as a separate base 64 encoded string. I need to get all of these pages into a single document again.
What I have attempted to do is to decode the Base64 string into an array. for each page of the document, I decode the Base64 string into a byte array using concatenation. I then use the File.WriteAllBytes method to create a single file. This file is a valid TIFF file and I am able to open it however only the last page appears. I have checked to make sure that the application I am using to open the document is capable of showing more than one page. I am using the Windows Photos application which will show all pages of a TIFF document.
How can I merge the pages of this document so that each page will appear correctly?
For example, the code below reads the Base64 string for each file and then combines them into a single output file. When I open bytefileout3.tiff however, I can only see the last page that was added into the document.
Dim inputPath As String = "C:\temp\file1.txt"
Dim fileStr As String = File.ReadAllText(inputPath)
Dim bytes As Byte() = Convert.FromBase64String(fileStr)
Dim inputPath2 As String = "C:\temp\file2.txt"
Dim fileStr2 As String = File.ReadAllText(inputPath2)
Dim bytes2 As Byte() = Convert.FromBase64String(fileStr2)
Dim bytes3 As Byte() = New Byte() {}
File.WriteAllBytes("c:\temp\bytefileout.tiff", bytes)
File.WriteAllBytes("c:\temp\bytefileout2.tiff", bytes2)
bytes3 = bytes2.Concat(bytes).ToArray()
File.WriteAllBytes("c:\temp\bytefileout3.tiff", bytes3)

copy file to directory in visual basic vb.net

I am tringing to copy files settings.copy from sourceDir to backupDir but getting error
Dim sourceDir As String = "c:\in\settings.copy"
Dim backupDir As String = "c:\out\"
File.Copy(sourceDir, backupDir)
while executing above script getting below error
System.IO.DirectoryNotFoundException: 'Could not find a part of the path 'c:\out\'.'
I already created c:\out\ folder
Have you read the documentation for File.Copy, or even just paid attention to Intellisense? Both arguments must be file paths. Neither can be folder paths.
On a related note, why do you have a variable named 'sourceDir' when it's clearly a file path and not a directory path? If you name things clearly - and particularly not misleadingly - then it's more likely that you'll avoid such mistakes. Of course, using the Help menu or F1 key to confirm that you're using a type of method correctly would help too.
Dim userprofile As String = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
Dim SystemDir As String = Environment.GetEnvironmentVariable("SystemDrive")
Dim sourceDir As String = "y\inbound\settings.exe"
Dim backupDir As String = "AppData\Local\user\default_user\"
Dim root As String = Path.GetPathRoot(userprofile)
Dim useDrpath As String = Path.Combine(userprofile, backupDir)
Dim SysDrpath As String = Path.Combine(SystemDir, root, sourceDir)
Dim file = New FileInfo("settings.cps")
file.CopyTo(Path.Combine(SysDrpath, useDrpath, file.Name), True)
My gole is to copy file from system installed driver to user profile driver
with above code i am able to copy file
c:\y\inbound\settings.exe C:\Users\pavan\AppData\Local\user\default_user\
please suggested any other better way to do above

How can I use decode encoded URL using VB.NET

I have an encoded url as follows;
dim inStr As String ="https%3A%2F%2Fmyweb%2page1%2Egov%2Emy%3A448%2Fserver%2FForm%3Fuserid%3Dtrouble"
I want to decode the url and assign this url to my webBrowser control.
From this thread:
Dim decodedUrl As String = HttpUtility.UrlDecode(encodedUrl)
I got a snippet to decode but am not using web application, am using windows application; can anyone suggest me the best way to do this?
you can use Uri.UnescapeDataString() method as follows:
Dim inputURL As String = "https%3A%2F%2Fmypage%2Eme%2Epage%2Esample%3A448%2Flfserver%2FForm%3Fuserid%3DTrouble"
Dim outputURL As String = Uri.UnescapeDataString(inputURL)
The output will be https://mypage.me.page.sample:448/lfserver/Form?userid=Trouble

How can I fetch the last-modified value of a remote file?

I'd like to know the last-modifed date of a remote file (defined via url).
And only download it, if it's newer than my locally stored one.
I managed to do that for local files, but can't find a solution to do that for remote files (without downloading them)
working:
Dim infoReader As System.IO.FileInfo = My.Computer.FileSystem.GetFileInfo("C:/test.txt")
MsgBox("File was last modified on " & infoReader.LastWriteTime)
not working:
Dim infoReader As System.IO.FileInfo = My.Computer.FileSystem.GetFileInfo("http://google.com/robots.txt")
MsgBox("File was last modified on " & infoReader.LastWriteTime)
I'd love to have a solution which will only have to download the headers of a file
You can use the System.Net.Http.HttpClient class to fetch the last modified date from the server. Because it's sending a HEAD request, it will not fetch the file contents:
Dim client = New HttpClient()
Dim msg = New HttpRequestMessage(HttpMethod.Head, "http://google.com/robots.txt")
Dim resp = client.SendAsync(msg).Result
Dim lastMod = resp.Content.Headers.LastModified
You could also use the If-Modified-Since request header with a GET request. This way the response should be 304 - Not Modified if the file has not been changed (no file content sent), or 200 - OK if the file has been changed (and the contents of the file will be sent in the response), although the server is not required to honor this header.
Dim client = New HttpClient()
Dim msg = New HttpRequestMessage(HttpMethod.Get, "http://google.com/robots.txt")
msg.Headers.IfModifiedSince = DateTimeOffset.UtcNow.AddDays(-1) ' use the date of your copy of the file
Dim resp = client.SendAsync(msg).Result
Select Case resp.StatusCode
Case HttpStatusCode.NotModified
' Your copy is up-to-date
Case HttpStatusCode.OK
' Your copy is out of date, so save it
File.WriteAllBytes("C:\robots.txt", resp.Content.ReadAsByteArrayAsync.Result)
End Select
Note the use of .Result, since I was testing in a console application - you should probably await instead.
If the server offers it, you can get it through the HTTP header Last-Modified property. But your still stuck at downloading the full file.
You could get it through FTP.
See if the server allows you to see the list of files in a folder.
If the website offer the date somewhere that you could pull through screen scrapping.
I know this is a little bit old question but, there's still a better answer.
Dim req As WebRequest = HttpWebRequest.Create("someurl")
req.Method = "HEAD"
Dim resp As WebResponse = req.GetResponse()
Dim remoteFileLastModified As String = resp.Headers.Get("Last-Modified")
Dim remoteFileLastModifiedDateTime As DateTime
If DateTime.TryParse(remoteFileLastModified, remoteFileLastModifiedDateTime) Then
MsgBox("Date Last Modified:" + remoteFileLastModifiedDateTime.ToString("d MMMM yyyy dddd HH:mm:ss"))
Else
MsgBox("could not determine")
End If