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");
Related
I am trying to import a text file into a rich text box for my Visual Basic project (Its a Windows Forms App (.Net Framework)). I copied the text information from the internet, and pasted it into notepad. Then I saved this in a folder I created called txtFiles inside the Bin folder of my project. However, when I used the FileOpen command, it gives me an error.
The code I used:
Dim strTemp As String = ""
rtbReference.Text = Nothing
FileOpen(1, "../txtFiles/Thriller.txt", OpenMode.Input)
Do Until EOF(1) = True
Input(1, strTemp)
rtbReference.Text = strTemp
Loop
FileClose(1)
Picture:
The error that I got when running my program
Any help would be appreciated, thank you so much
There are more easier methods of working with files in the 21st century. I mean the IO namespace provides a handful of methods.
Try any of the following. Add this at the top of your code file
Imports System.IO
Method 0: File.ReadAllText
Since the file you want to load is a plain tet file, I suggest you use this.
RichTextBox1.Text = File.ReadAllText(pathtofile)
Method 1: Richtextbox.LoadFile()
Someone (or something) has already posted the link to an article on this in the comments but i'm pretty sure you've not gone through it. Well here's how to use it
RichTextBox1.LoadFile(pathtofile)
I use this method for only rtf files but it was completely my decision.
Method 2 To n - 1
You need to know how to use a search engine as there are many methods out there that can be used such as the StreamReader and so on.
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 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.
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 developing an application in vb.net. You drag any type of file onto the exe, and a window pops up with some options for the file, then it saves the file to a different location, works some SQL magic, etc. It works great for the most part.
The only issue I've found is that if the path of the file contains any spaces, the application will crash immediately with the error window: http://i.stack.imgur.com/mVamO.png
I'm using:
Private filename as String = Command$
This is located right inside my form's class declaration, not within a sub/function.
Without this line, my program runs fine (although useless, without accessing the file).
I've also tried (I think this was it, I don't have the code with me at the moment):
Private filename as String = Environment.CommandLine
And it had the same issue.
So, in vb.net, is there a way to drag a file onto an exe and use that path name, even if there are spaces in the path name?
Windows will put double-quotes around the passed command line argument if the path to the dragged file contains spaces. Trouble is, you are using an ancient VB6 way to retrieve the argument, you see the double quotes. Which .NET then objects against, a double quote is not valid in a path name. Use this:
Dim path = Command$.Replace("""", "")
Or the .NET way:
Sub Main(ByVal args() As String)
If args.Length > 0 then
Dim path = args(0)
MsgBox(path)
'' do something with it..
End If
End Sub
If possible, do post your code as it's pretty much anything that can go wrong. Normally, after receiving CommandLine Arg, I would try to use a System.IO.File wrapper and use built-in mechanisms to verify file and then proceed with it further using IO as much as possible. If you are attempting to directly manipulate the file, then the spaces might become an issue.
In addition, there is a way to convert long file path + name to old DOS’s 8.3 magical file path + name. However, I’ll go into R&D after I see what you are doing in code.