PDA

Click to See Complete Forum and Search --> : [2.0] DLL Import


Rattlerr
Jul 24th, 2006, 06:06 PM
I need to import a DLL using the Class.cs...this is what i have but its not correct..


static [System.Runtime.InteropServices.DllImport("dll file name")]


The Other issue i'm having is i used the Covert Tool in Visual Studio to upgrade a VB6 project to VB.NET,the Problem i having is with the F = FreeFile,
F = Split(LTD, ":")....

Here is the VB6 Code:


Dim LTD As String, F As Variant, x As Integer

F = FreeFile
With CommonDialog1
.FileName = ""
.DialogTitle = "Load bots List"
.Filter = "All Supported Types|*.txt"
.ShowOpen
If .FileName = "" Then Exit Sub
Open .FileName For Input As #F
While Not EOF(1)
Input #1, LTD
F = Split(LTD, ":")
If x < 1000 Then
Set Item = ListView1.ListItems.Add(, , , , 2)
Item.SubItems(1) = F(0)
Item.SubItems(2) = F(1)

Now when i used the Convert tool in VS 2005 to VB.NET the F = FreeFile was changed too:

F = FileSystem.FreeFile

I know the FileSystem is refering to the VB FileSystem used in Visual Basic .NET..I need to get that Over into C# along with the F = Split(LTD, ":"),
everything else i have already Reworked into C#.. :)

jmcilhinney
Jul 24th, 2006, 06:33 PM
You should rewrite that section from scratch using the System.IO namespace. If you're reading text then use a StreamReader. The .NET 2.0 IO.File class also has methods like ReadAllLines. This allows you to create a string array of all the lines in a text file in one line of code. It uses a StreamReader under the covers. Once you have the array you can then iterate over it and split each element using String.Split and create your ListViewItems from that.

Rattlerr
Jul 24th, 2006, 06:40 PM
Ok,so the openFileDialog isnt a good idea than ehh??

StreamReader sr = File.OpenText("Client.txt");
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All Files (*.*) | *.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
openFileDialog1.Title = "Load Client List";
openFileDialog1.FileName = "";
while ((input = sr.ReadLine()) != null)
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (sr.ReadLine())
{
//insert code to read the stream here
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could Not Read File error: " + ex.Message);
}
}

jmcilhinney
Jul 24th, 2006, 06:59 PM
If you want the user to select a file then the OpenFileDialog is required, but you can choose to read the file line by line with your own StreamReader or let the File class do it for you, then loop through the array it returns. You've got some problems with that last code though. It should be something more like:using (OpenFileDialog ofd = new OpenFileDialog())
{
// Set properties of 'ofd' here.

if (ofd.ShowDialog() == DialogResult.OK)
{
using (StreamReader sr = new StreamReader(ofd.FileName))
{
while (sr.Peek() != -1)
{
// Read a line of text and split it, then use it to create a ListViewItem.
this.listView1.Items.Add(new ListViewItem(sr.ReadLine().Split(':')));
}

sr.Close();
}
}
}

Rattlerr
Jul 24th, 2006, 07:22 PM
So this line of code here:

this.listView1.Items.Add(new ListViewItem(sr.ReadLine().Split(':')));


Replicates the F = Split(LTD, ":") ?????

In the Text file for example the Username and Password are suppose to be like this:

username : password if its not like that the program returns an Error...And only the Username show up in the listview1 with the Small Icon for Online & Offline...



The other Question i have is the F = FreeFile...When i upgraded it to the VB.NET it came out like F = FileSystem.FreeFile,Which in C# FileSystem is used its Directory...So how would i replicate that into C#???


Directory FileSystem = new Directory(); // ????
or
struct Directory
{
F,
FileSystem,
FreeFile,
}

This is where F is used again = F(0) the 0 & 1 is a SmallIcon

Item.SubItems(1) = F(0) // VB6
Item.SubItems(2) = F(1)


I'm just not sure how to Replicate F = FreeFile or F = FileSystem.FreeFile,
I know the FileSystem is equal to Directory in C#..

jmcilhinney
Jul 24th, 2006, 07:50 PM
I don't see what use that "F = FreeFile" is anyway because before it's used again F is assigned a different value. Forget the VB6 code altogether. Don't even think about "converting". Just think about what you're trying to achieve and how best to do that in C#. What do you need to happen that my code in post #4 doesn't do?

Rattlerr
Jul 24th, 2006, 08:01 PM
Your right i'm just confusing myself all together....The only problem i need to Resolve is:


Open .FileName For Input As #F

Item.SubItems(1) = F(0)
Item.SubItems(2) = F(1)


Error 2 The name 'F' does not exist in the current context

jmcilhinney
Jul 24th, 2006, 08:08 PM
You're still trying to convert VB6 code. What is the object that you want to achieve? Is it that you want to open a text file? If so then creating the StreamReader in my code does that. Is it creating a ListView Item? If so then my code does that also. You haven't said what you're trying to achieve that the code I supplied doesn't do. My code prompts the user to select a file. You'll have to add code to set up the OFD as you want it. It then opens the selected file as a text file. It then reads each line, splits them on the colon character and creates a ListViewItem where each substring is in its own column. What else is it that you want?

Rattlerr
Jul 24th, 2006, 08:25 PM
ok the SubItems(0) is a Online SmallIcon, (1) is a Offline SmallIcon that shows up in the listView1 next to the username...

Item = ListView1.ListItems.Add(, , , , 2)
Item.SubItems(1) = F(0) // F produces the Error Below
Item.SubItems(2) = F(1)

Error 2 The name 'F' does not exist in the current context

Your Code is what i needed, this is a Different issue m8.. :)

So my question is what would i declare F as ??

int F; ???
public string F; ??

Rattlerr
Jul 24th, 2006, 08:33 PM
I generated a method Stub for F ,that dropped the Error for now..