Jump to content
Nytro

[C#] Creating and using DLL Files

Recommended Posts

Creating and using DLL Files

by sunjester

Using DLL files is to eliminate writing code over and over again. DLL's are often used for may things like file I/O. I will show you how to take the first two tutorials I've written in this section (read and writing text files) and put them both in a DLL file. Then, once the DLL is in our project I will show you how to use the read and write methods we placed inside the DLL.

It's probably more feasible for .NET applications to utilize DLL's instead of rewriting so much code. C# and VB .NET applications are mostly used ("in the industry") for demo applications, or test applications for rapid application development. yes, c#, and VB.net are RAD languages just like the old VB6.

1. first, create a new project. Open the wizard and select "Class Library" and give it an appropriate name

mkyk2h.png

2. here you can copy & Paste the code from the previous two tutorials, below is what mine looks like now.

//sunjester
//fusecurity.com
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;

namespace FileIO
{
public class InputOutput
{
//writing to text files
public void writeToFile(string fileName, string content)
{
StreamWriter write = new StreamWriter(fileName);
write.Write(content);
write.Close();
}

//reading from text files
public ArrayList Read(string fileName)
{
StreamReader read = new StreamReader(fileName);
ArrayList lines = new ArrayList();
while (!read.EndOfStream)
{
lines.Add(read.ReadLine());
}
read.Close();
return lines;
}
}
}

3. now we can build the DLL, so in the menu select "Build" then "Build Solution".

4. next, let's go ahead and add another project to this one just in case we need to go back to the original DLL source and update it.

120jwra.png

5. name it accordingly.

ml6w48.png

6. now we add the reference to our DLL file we just created.

2cwsz1e.png

5fngwn.png

7. and the final code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FileIO;
using System.Collections;

namespace UseFileIO2
{
class Program
{
static void Main(string[] args)
{
InputOutput io = new InputOutput();
io.writeToFile("c:\\test44.txt", "here is some sample test data to write");
ArrayList lines = io.Read("c:\\test44.txt");
for (int i = 0; i < lines.Count; i++)
{
Console.WriteLine(lines[i]);
}
}
}
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...