Sign in to follow this  
0xwarning

Learn C# | Working With Files

Recommended Posts

If you have ever wanted to learn c# for multiple purposes.

Then here is your answer.

 

[hide]

Lets Get Started

 

You Will Need

Visual Studio.

Make Sure To Have C# Installed.

 

So I'm Currently using a windows form modified by devexpress.

 

Have A Layout of what you want but

here is mine for an example.

Make A Listview, Two Labels And Two Buttons

 

Now Add Three Columns In The List View

 

Like

This

 

 

Code

Button To Grab The Text File

try
            {
                OpenFileDialog ofd = new OpenFileDialog(); // Create New OFD
                ofd.Filter = "TXT files|*.txt"; // Only Accept Txt
                ofd.Title = "Load Accounts"; // Title Of OFD
                var dialogResult = ofd.ShowDialog(); // OFD ShowDiag Var
                if (dialogResult == DialogResult.OK) // If Ok
                {
                    foreach (var line in System.IO.File.ReadLines(ofd.FileName)) // Read LInes Of File
                    {
                        if (line.Contains(":")) // if Contaions
                        {
                            string Username = line.Split(':')[0].Trim(); // Splits example ( user:password:rank ) Into ( user )
                            string Password = line.Split(':')[1].Trim(); // Splits example ( user:password:rank ) Into ( password )
                            string Extra = line.Split(':')[2].Trim(); // Splits example ( user:password:rank ) Into ( Rank )
                            string[] row = { Username, Password, Extra }; // Make a row to be added into collums ( user password rank )
                            var listViewItem = new ListViewItem(row); // Make A New Var To Add Row
                            listView1.Items.Add(listViewItem); // Finally Add The Items
                        }
                    }
                }
                Statuslbl.Text = "Status > Success";
            }
            catch
            {
                Statuslbl.Text = "Status > Failed";
            }

Button To Remove Dupes

           try
           {
               var streamR = new StreamReader(File.OpenRead(@"users.txt")); // File We Are Removing Dupes From

               var streamW = new StreamWriter(File.OpenWrite(@"usersDupeRemove.txt")); // New File We Write 

               var lines = new HashSet(); 

               while (!streamR.EndOfStream) // While Not EndOfStream
               {
                   string line = streamR.ReadLine(); // ReadLine
                   int hashcode = line.GetHashCode();  // Create Hash Code var & Get Hash Code
                   if (lines.Contains(hashcode)) // If Contains Hash
                   {
                       continue;
                   }
                   lines.Add(hashcode); 
                   streamW.WriteLine(line); // Write Whats stored in line
               }
               streamW.Flush(); // Clear Buffers
               streamW.Close(); // Close Writer
               streamR.Close(); // Close Reader
               Weblbl.Text = "File > Cleaned";
           }
           catch
           {
               Weblbl.Text = "File > Failed";
           }

Hope You Have Enjoyed This Thread.

Please Leave Feedback

 

[/hide]

 

Share this post


Link to post
Share on other sites

Whaile thats prob. nice

Share this post


Link to post
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.
Note: Your post will require moderator approval before it will be visible.

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.

Sign in to follow this