I am new in vb 2012 and when I tried to apply VB6 source of the two differ, I've been searching in google how to use this source vb 2012 but I do not find it, anyone there who can help me how to use this source in vb 2012, the following source code is
Dim FileTeks As String
FileTeks = App.Path & "\conn.ini"
UserName = Trim(txtUserName.Text)
Pass = Trim(txtPassword.Text)
Server = Trim(txtServer.Text)
Open FileTeks For Output As #1
Print #1, Enkrip(Server, 4)
Print #1, Enkrip(UserName, 4)
Print #1, Enkrip(Pass, 4)
Close #1
thanks for the help
Here is one way to write to file in .NET
Using sw As New System.IO.StreamWriter("FileTeks.ini", False)
sw.WriteLine(Enkrip(Server,4))
sw.WriteLine(Enkrip(UserName,4))
sr.WriteLine(Enkrip(Pass,4))
sw.Flush()
sw.Close()
End Using
This assumes the output from Enkrip is string. Alternatively (since it is an ini):
sw.WriteLine("{0} = {1}", "Server", Enkrip(Server,4))
' or
sw.WriteLine("{0} {1}", "Server =", Enkrip(Server,4))
You probably need the 'keys' in the clear so you can read them back again rather than encrypting the entire line. Also you probably want to put it all in a Try/Catch so that of the filename is bad or there is an issue with permissions you can trap it.
Another alternative may be to use PInvoke to use the old Win32 Read/WritePrivateProfileString.
HTH
Using the VisualBasic package from .net you should be able to find the most of the VB6 own commands. In any case, vb.net is not a merely upgrade of vb6, it is a quite different language.
If your code base is not too big, I will recommend you to try to refactor your code. We made once one automatic conversion from vb6 to vb.net and now we regret it.
Related
I am trying to create a macro that will export a Microsoft Project file into an excel file. Through the use of macro recording I have got a line of code that accomplishes this using the export wizard, but I want the file path and file name to be dynamic so I can use this macro on different projects. I have been searching many other threads and the Microsoft website with no luck. Is this possible?
Here is what I have:
sub formatAndSave ()
FileSaveAs Name:="C:\Users\XXXXXX\SharePoint\Projects\ProjectType\HxH\myProject.xlsx",_
FormatID:="MSProject.ACE", map:="myMap"
end sub
One idea I tried was:
Active.Workbook.SaveAs FileName:=Title
Any help would be very much appreciated!
For the sake of simplicity, let's assume for all answers below your project is located at c:\projects\myProj.mpp
I think you're after the string replace function. Something like:
Dim excelFilePath As String
excelFilePath = Replace(ActiveProject.FullName, ".mpp", ".xlsx")
Debug.Print excelFilePath
'the output would be c:\projects\myProj.xlsx
If you're unfamiliar with string manipulation in VB/VBA, just search the web for "VBA string manipulation". Microsoft has a decent article here: https://msdn.microsoft.com/en-us/library/aa903372(v=vs.71).aspx
A few other things that may be handy for you are these variables:
ActiveProject.FullName 'shows full path & name, so you'd get "c:\projects\myProj.mpp"
ActiveProject.Path 'shows just the path, so you'd get "c:\projects\"
ActiveProject.Name 'shows just the file name, so you'd get "myProj.mpp"
Finally, one caveat I've seen is that the ActiveProject.FullName and ActiveProject.Name variables may or may not provide the file extension depending on your local windows environment settings. I've observed that if Windows Explorer is configured to hide file extensions, then these variables also withhold the extension; if Explorer is configured to show them, then they are provided in the variables. Make sure your code is robust to both cases, or make sure you have control over the environment where you code will run.
I am trying to add the path and file name of files that throw exceptions (see image attached). My code works but Visual Studio Community Edition 2015 (VB.NET 4.6) reports "Variable 'FileName' is used before it is assigned a value...". Can anyone suggest how I can remove this issue, or whether this is ok to ignore? I am new to VB.NET and would be happy to reformat my code if there is a better way to do this.
Regards
George
use this:
Dim FileName as String = Nothing 'String.Empty 'C#: null
I've been trying to find a way to render \r\n as actual newlines in the immediate and command window in my VB.NET 4.5.1 application. I came across this question which teaches about the nq specifier, but it appears to apply only to C#. In VB.NET, nq doesn't even appear to work because I get Expression expected printed back out at me.
Is there a different way to make newlines actually show as separate lines in the immediate or command window in VB.NET?
An easier way in visual studio 2015 is to do this in the debug window:
?string,nq
You can also do the debug version in earlier versions of VS
?System.Diagnostics.Debug.Print(string,nq)
I've discovered that the solution is to use System.Diagnostics.Debug.Print(). To reproduce the original issue, entering this into the Immediate window...
? "a" & vbCrLf & "a"
...just returns this:
"a a"
However, using this...
System.Diagnostics.Debug.Print("a" & vbCrLf & "a")
...actually shows the newline:
a
a
I'm pretty new to visual basic (and coding in general) so if I've made any really simple mistakes let me know.
Right now, I'm getting a pretty weird problem with my vb.net code.
The filestream is able to correctly open the file and read from it - but what's weird is that while the code is able to read a bunch of lines from the beginning of the file, when I manually open the file in notepad I'm not. Here's the code:
Dim fs, f, s 'filesystemobject, file, stream.
fs = CreateObject("Scripting.FileSystemObject")
f = fs.GetFile(CurrDataPath) ' This change made to ensure the correct file is opened
s = f.OpenAsTextStream(1, 0) ' 1 = ForReading, 0 = as ASCII (which i think is right?)
Dim param(14) As String
Dim line As String
line = s.ReadLine()
While i <= 14
i += 1
MessageBox.Show(line)
line = s.ReadLine()
End While
(I've read that arrays are a bad idea but they've been convenient and haven't caused me any problems so I've been using them anyways.)
What's weird is that when this code is run, it will (in the message boxes) show me the information I want to see - which isn't bad at all. The information that I want looks like this:
BEGINPARAM
parameter1, 0
parameter2, 7.5
ENDPARAM
EDIT:
After using Path.GetFullPath(DFile), I found that there were two files in different directories with the same name DFile. The file I had been opening in Notepad was saved in the directory where I expected it to be saved, while the file the code was reading was saved in the VB project's folder.
Once I changed the code to rely on CurrDataPath which includes the expected path, the code read from the file exactly what I did in notepad.
I do have word wrap on in notepad, so I know that's not the issue, however, I will look into getting notepad++.
The file named DFile is created in a c++ program that I'll be digging through to find out why one part of the file is written to a different folder than the rest.
Obviously I'm missing something important, and if anyone could help, that would be great.
*Note: This is a vb6 migration project so if anyone asks I can provide the old code.
Assuming the most recent version of VB.Net, the modern way to write that is like this:
For Each line As String In File.ReadLines(CurrDataPath).Take(14)
MessageBox.Show(line)
Next
I'm not 100% clear on what you're saying. There's nothing in this code that outputs to a file, so what you have to be saying is that when you open the file referenced by "DFile" on line 3 above, that file doesn't have the lines containing "parameter1, 0" and "parameter2, 7.5" in it?
Since we know that's not technically possible, do verify the answer to the question above and make sure you're really opening the same file in notepad as the script is opening. The second thing to do is to turn on Word Wrap in Notepad or download Notepad++ (a text editor I think everyone should have anyway) and make sure that the data's actually missing, and not just not showing on your screen because it's not using Windows style line endings.
I'm converting an old VB6 app that's been chugging along for years, but has a server name hard-coded into it. We want to convert it to a C# app, and I'm doing OK reading most of the VB6 code, except I'm not 100% sure I'm reading the code right when it comes to some basic File IO operations.
Several lines of code call the FreeFile function. I see that VB.NET has the function, and I've read the documentation but it's still not making sense to me.
Remarks
Use FreeFile to supply a file number that is not already in use.
Question 1: All of my searching is not telling me what a "file number" is or what it's used for. Is this some way of referring to a file, but not by file name?
Question 2: I'm also not sure about the Dir function. To me it looks like it's equivalent to System.IO.File.Exists(), is that right?
Example code:
If Not Dir(My.Application.Info.DirectoryPath & "\path.txt") = "path.txt" Then
End
Else
iFile = FreeFile
FileOpen(iFile, My.Application.Info.DirectoryPath & "\path.txt", OpenMode.Input)
Input(iFile, lsDataIn)
FileClose()
End If
FreeFile is not needed in C#, since it has been completely abstracted away. Just open your files in C# using the standard File I/O.
As far as Dir function goes, it was very versatile in VB6 and did a number of things. In this context, yes, you are correct, it can be replaced with File.Exists.
So your code in C# could look like this:
if (System.IO.File.Exists("path.txt"))
Application.Exit();
else
string txt = System.IO.File.ReadAllText("path.txt");