I have written this code for saving decoded images locally and it works fine. Actually I need to save this decoded image in a server, but I don't now how to achieve this? I have seen many examples saving files in server, but here I have a base64 decoded image ... Can I get any hint ? Thanks in advance
Dim bt64 As Byte() = System.Convert.FromBase64String(srcFile)
Dim destFile As String = " C:\Users\Administrator\Desktop\MASavedImages"
Dim imgName As String
imgName = String.Format("{0:dd-MM-yyyy hh-mm-ss tt-fff}", DateTime.Now)
imgName += ".jpg"
If (Not System.IO.Directory.Exists(destFile)) Then
System.IO.Directory.CreateDirectory(destFile)
File.WriteAllBytes(destFile + "\" + imgName, decodedimg)
Else
File.WriteAllBytes(destFile + "\" + imgName, decodedimg)
End If
And why can't you save to server by the same method ? What happens when you change the destFile to \\server\path\filename and run ?
Related
I have an Arabic expression decoded using base64 and should be encoded in a function using vb.net, then filled in a dataset to be displayed in crystal report.
Now the main problem is the below:
The retrieved data is decoded in base64 but I am not able to encoded and display it in Arabic properly. I have used 3 ways, but all of them are not displaying characters properly; If I take the expression and paste in an online converter, it will converted properly (so without doubts the expression was decoded correctly).
Dim test = "2YbYtSDZgdmKINin2YTZhNi62Kkg2KfZhNi52LHYqNmK2Kk="
Dim data = System.Convert.FromBase64String(test)
Dim endodedData = System.Text.ASCIIEncoding.ASCII.GetString(data)
returns: ???? ???? ?????????? ??????????????
Dim win= Encoding.GetEncoding("windows-1256")
Dim endodedData2 = win.GetString(data)
returns: ظ†طµ ظپظٹ ط§ظ„ظ„ط؛ط© ط§ظ„ط¹ط±ط¨ظٹط©
Dim iso = Encoding.GetEncoding("ISO-8859-6")
Dim endodedData3 = iso .GetString(data)
returns:
ع" & ChrW(134) & "ظ ع" & ChrW(129) & "ع" & ChrW(138) & " ظع" & ChrW(132) & "ع" & ChrW(132) & "ظظ ظع" & ChrW(132) & "ظظظع" & ChrW(138) & "ظ
The expected expression that should be returned should be :
نص في اللغة العربية
Any help?
Ok I got it :
Dim endodedData4 = Encoding.UTF8.GetString(data)
I'm trying to check if the file exists in the directory in which the application was saved and if so, to add a number at the end -1, -2. -3 - based on whether a file with the same name already exists. My code is below:
Dim FileName, FilePath As String
Dim FileNumber As Integer
FileName = ProjectName
FilePath = Path.Combine(CurrentDirectory, FileName)
If File.Exists(FilePath) = True Then
Do While File.Exists(FilePath)
FileNumber = FileNumber + 1
FileName = FileName & "-" & FileNumber
FilePath = Path.Combine(CurrentDirectory, FileName)
Loop
End If
NewWorkbook.SaveAs(FilePath)
When I run this code and the file is saving the first time, it works as intended but if I try saving the file with the same name a second time, there is no iterated FileNumber added to it, so the file name stays the same and it cannot save without replacing the original file.
Why is the File.Exists not recognizing that this file already exists and how can I fix this?
There is a logical problem in your code. You continue to modify the same variable and building continuosly new names.
For example. Suppose to have initially a file with the name "Project.vb". At the first iteration inside the loop you check for a file named "Project.vb1", if your loop continues at the second iteration you check for a file named "Project.vb12" and so on.
A more correct way could be
Dim FileName, FileWithoutExtension, FileExtension, FilePath As String
Dim FileNumber As Integer = 1
Dim currentDirectory As String = "E:\temp" ' as an example
FileName = "test.txt" ' as an example
FileExtension = Path.GetExtension(FileName)
FileWithoutExtension = Path.GetFileNameWithoutExtension(FileName)
FilePath = Path.Combine(CurrentDirectory, FileName)
' No need of additional if to test file existance.
Do While File.Exists(FilePath)
FileNumber = FileNumber + 1
' Rebuild the Filename part wtih all the info
FileName = FileWithoutExtension & "-" & FileNumber.ToString("D3") + FileExtension
FilePath = Path.Combine(CurrentDirectory, FileName)
Loop
NewWorkbook.SaveAs(FilePath)
I copy some test images from source folder.. but i want to make their image name become "001,002" .. and so on.
For Each path As ListViewItem In listbat1.Items
For Each Ftif As String In Directory.GetFiles(path.SubItems(0).Text, "*.tif")
'For n As Integer = 0 To listbat1.Items.Count - 1
Dim Finfo As New FileInfo(Ftif)
My.Computer.FileSystem.CopyFile(Ftif, txtdirectory.Text & imgdir & Finfo.Name & ".tif")
'Next
Next
Next
Is it possible ? if so, can you help me? pls ..
If you want to rename your file using a progressive counter, then you could easily do it using the ToString with a formatting expression. In this example D3 means convert the input number to a string using three numbers and padding with zero if the number is not converted to enough character.
Dim counter as Integer = 0
For Each path As ListViewItem In listbat1.Items
For Each Ftif As String In Directory.GetFiles(path.SubItems(0).Text, "*.tif")
Dim Finfo As New FileInfo(Ftif)
Dim destFile = Path.Combine(txtDirectory.Text, imgdir, counter.ToString("D3") + ".tif")
My.Computer.FileSystem.CopyFile(Ftif, destFile)
Counter = Counter + 1
Next
Next
Notice also that building a path should always be done using the Path class
Okay, so I'm working in VB.NET, manually writing error logs to log files (yes, I know, I didn't make the call). Now, if the files are over an arbitrary size, when the function goes to write out the new error data, it should start a new file with a new file name.
Here's the function:
Dim listener As New Logging.FileLogTraceListener
listener.CustomLocation = System.Configuration.ConfigurationManager.AppSettings("LogDir")
Dim loc As String = DateTime.UtcNow.Year.ToString + DateTime.UtcNow.Month.ToString + DateTime.UtcNow.Day.ToString + DateTime.UtcNow.Hour.ToString + DateTime.UtcNow.Minute.ToString
listener.BaseFileName = loc
Dim logFolder As String
Dim source As String
logFolder = ConfigurationManager.AppSettings("LogDir")
If ex.Data.Item("Source") Is Nothing Then
source = ex.Source
Else
source = ex.Data.Item("Source").ToString
End If
Dim errorFileInfo As New FileInfo(listener.FullLogFileName)
Dim errorLengthInBytes As Long = errorFileInfo.Length
If (errorLengthInBytes > CType(System.Configuration.ConfigurationManager.AppSettings("maxFileSizeInBytes"), Long)) Then
listener.BaseFileName = listener.BaseFileName + "1"
End If
Dim msg As New System.Text.StringBuilder
If String.IsNullOrEmpty(logFolder) Then logFolder = ConfigurationManager.AppSettings("LogDir")
msg.Append(vbCrLf & "Exception" & vbCrLf)
msg.Append(vbTab & String.Concat("App: AppMonitor | Time: ", Date.Now.ToString) & vbCrLf)
msg.Append(vbTab & String.Concat("Source: ", source, " | Message: ", ex.Message) & vbCrLf)
msg.Append(vbTab & "Stack: " & ex.StackTrace & vbCrLf)
listener.Write(msg.ToString())
listener.Flush()
listener.Close()
I have this executing in a loop for testing purposes, so I can see what happens when it gets (say) 10000 errors in all at once. Again, I know there are better ways to handle this systemically, but this was the code I was told to implement.
How can I reliably get the size of the log file before writing to it, as I try to do above?
Well, as with many things, the answer to this turned out to be "did you read your own code closely" with a side order of "eat something, you need to fix your blood sugar."
On review, I saw that I was always checking BaseFileName and, if it was over the arbitrary limit, appending a character and writing to that file. What I didn't do was check to see if that file or, indeed, other more recent files existed. I've solved the issue be grabbing a directory list of all the files matching the "BaseFileName*" argument in Directory.GetFiles and selecting the most recently accessed one. That ensures that the logger will always select the more current file to write to or -if necessary- use as the base-name for another appended character.
Here's that code:
Dim directoryFiles() As String = Directory.GetFiles(listener.Location.ToString(), listener.BaseFileName + "*")
Dim targetFile As String = directoryFiles(0)
For j As Integer = 1 To directoryFiles.Count - 1 Step 1
Dim targetFileInfo As New FileInfo(targetFile)
Dim compareInfo As New FileInfo(directoryFiles(j))
If (targetFileInfo.LastAccessTimeUtc < compareInfo.LastAccessTimeUtc) Then
targetFile = directoryFiles(j)
End If
Next
Dim errorFileInfo As New FileInfo(listener.Location.ToString() + targetFile)
Dim errorLengthInBytes As Long = errorFileInfo.Length
I have a string of text i captured within AutoCAD (0.000000, 0.000000, 0.000000) wich is saved to a text based file named position.txt.
as you probably have gatherd with a file name such as position.txt the text could be composed of any random number combination eg: (5.745379, 0.846290, 150.6459046).
However for it to be of any use to me I need the captured string to exist without spaces or brackets how can i achiev this in VB.net?
Use String.Replace. Its probably not the most efficient way but it will get the job done.
Dim file as String = My.Computer.FileSystem.ReadAllText("position.txt")
Dim output as String = file.Replace(" ", "") _
.Replace("(", "") _
.Replace(")", "")
My.Computer.FileSystem.WriteAllText("output.txt", output, false)
as above
s = "(5.745379, 0.846290, 150.6459046)"
s = s.replace("(","")
s = s.replace(")","")
and then
dim answer() as string = s.split(",")
dim number as double
For each a as string in answer
if double.tryparse(a,n) then
console.writeline(n.tostring & " is a number")
else
console.writeline(n.tostring & " is rubbish")
next