6 Replies - 306 Views - Last Post: 19 December 2013 - 02:44 PM Rate Topic: -----

#1 TechCP  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-December 13

I need to import from a textbox to listbox but adding a variable. [?]

Posted 19 December 2013 - 11:37 AM

Alright, I created this program so I didn't have to manually remove all of the virus/spyware/adware files and folders that I remove on a regular basis. (I run a computer repair shop.)

Right now my code is as follows: (excerpt)
//Detect if folder exists
string adconduit = @"C:\Users\" + Environment.UserName + "\\AppData\\Local\\Conduit";
                if (Directory.Exists(adconduit))
                {
//Add to textbox of files/folders found
                    textBoxX1.Text = textBoxX1.Text + Environment.NewLine + adconduit.ToString();
//Delete folder
Directory.Delete(adconduit, yes);
                }


Which works perfectly fine.
Except for when I want to update what I want it to search for, I have to send out an update for the entire .exe and I'd rather it read the list from a text file.

(If there's a better/more efficient way of doing this I'm open to suggestions.)

Now I have a text file with this info:
C:\Program Files (x86)\FunWebProducts
C:\Program Files (x86)\ScorpionSaver
C:\Program Files (x86)\SiteAdvisor
C:\Program Files (x86)\MyPC Backup
C:\Program Files (x86)\YTD Toolbar
C:\Program Files (x86)\Ask.com
C:\Program Files\ScorpionSaver Services
C:\Users\Gayle\AppData\LocalLow\facemoods.com
C:\Users\Gayle\AppData\LocalLow\Search Settings
C:\Users\Gayle\AppData\Roaming\.#
C:\Users\Gayle\AppData\Roaming\FrostWire
C:\Users\Gayle\AppData\Roaming\Happyville__
C:\Users\Gayle\AppData\Roaming\PlayFirst
C:\Users\Gayle\AppData\Roaming\redsn0w
C:\Users\Gayle\AppData\Roaming\UClick
C:\Users\Gayle\AppData\Roaming\Uniblue
C:\Users\Gayle\AppData\Roaming\UpdaterEX
C:\Users\Gayle\AppData\Roaming\WildTangent


Now if I wanted to import all those at the end with:
("C:\Users\" + Environment.UserName + "\AppData\Roaming\redsn9w");



How would I do that?

Is This A Good Question/Topic? 0
  • +

Replies To: I need to import from a textbox to listbox but adding a variable. [?]

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9096
  • View blog
  • Posts: 34,161
  • Joined: 12-June 08

Re: I need to import from a textbox to listbox but adding a variable. [?]

Posted 19 December 2013 - 11:54 AM

Have the "Environment.UserName" part of the string be a recognizable chunk... like [username] or [foo].. then read the line, replace your string "[username]" or "[foo]" with Environment.UserName.

Voila.

http://msdn.microsof...=vs.110%29.aspx
Was This Post Helpful? 1
  • +
  • -

#3 TechCP  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-December 13

Re: I need to import from a textbox to listbox but adding a variable. [?]

Posted 19 December 2013 - 12:12 PM

View Postmodi123_1, on 19 December 2013 - 11:54 AM, said:

Have the "Environment.UserName" part of the string be a recognizable chunk... like [username] or [foo].. then read the line, replace your string "[username]" or "[foo]" with Environment.UserName.

Voila.

http://msdn.microsof...=vs.110%29.aspx

Hahaha, it's like magic!
Thanks man.

Not entirely sure why I didn't think of that earlier.
Putting the blame off on coder's block.
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4469
  • View blog
  • Posts: 7,780
  • Joined: 08-June 10

Re: I need to import from a textbox to listbox but adding a variable. [?]

Posted 19 December 2013 - 12:34 PM

Rather than just expecting the user's profile and paths to be identical on all machines, use the Environment variable to get paths like AppData and Program Files/Program Files (x86):

http://msdn.microsof...(v=vs.110).aspx

It's much more "safe", because users can modify their own special paths. Using this method will have the path given to you, no matter what it is.

Also, use Path.Combine for concatenating paths. Keeps you from having to worry about putting the pieces together correctly. Also safer.

You can still use Modi's advice, just represent whole paths with your tokens. Example:

ProgramFiles|SomePathPart\SomeOtherPart\
ProgramFiles86|Blah\Blah
AppData|etc\etc


And then you can use .Split to split it around the "|" character (or some other path-illegal character so you can guarantee there's only one to split).
Was This Post Helpful? 0
  • +
  • -

#5 TechCP  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-December 13

Re: I need to import from a textbox to listbox but adding a variable. [?]

Posted 19 December 2013 - 01:35 PM

Awesome. Thank you.

Right now I'm using:
 txtFound.Items.Clear();

            {
                string[] allLines = textBox2.Text.Split('\n');

                foreach (string text in allLines)
                {
                    if (Directory.Exists(text))
                    {
                        txtFound.Items.Add(text);
                    }
                }

                string[] destination = new string[txtFound.Items.Count];
                txtFound.Items.CopyTo(destination, 0);

                if (txtFound.Items.Count.ToString() == "0")
                {
                    MessageBox.Show("The scanner found no malicious content.", "System clean!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

                else
                {
                    if (MessageBox.Show("Scanner found " + txtFound.Items.Count.ToString() + " cases of malicious software. \nWould you like to remove it now?", "System needs cleaning", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) == DialogResult.Yes)
                    {
                        foreach (string removethese in destination)
                        {
                            if (Directory.Exists(removethese))
                            {
                                Directory.Delete(removethese);
                            }
                        }
                    }
                }
            }

            string[] cprlog = new string[txtFound.Items.Count];
            txtFound.Items.CopyTo(cprlog, 0);

            foreach (string logitems in cprlog)
            {
                txtLog.Text = txtLog.Text + "\n" + logitems.ToString();
            }

            File.WriteAllText(@"C:\CPR_log" + DateTime.Now.ToString("dd-MM-yyyy hh-mm-ss") + ".txt", "Viruses/Spyware/Adware successfully removed:" + Environment.NewLine + Environment.NewLine + txtLog.Text);


So for my code I should have a public string gathering their special folder, then have my text file not including their special folder, correct?
foreach blah in blah2
listBox1.Items.Add(specialfolderstring + blah);



That will get their correct special folder and still get the locations from my text file, correct?
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4469
  • View blog
  • Posts: 7,780
  • Joined: 08-June 10

Re: I need to import from a textbox to listbox but adding a variable. [?]

Posted 19 December 2013 - 02:33 PM

Close. You should still use Path.Combine for combining paths.

Also, I'd advise against using the UI control as the place to store your paths. You should read them all into a string array (as you're doing), then select the valid, completed ones out of the array. I'll show you an example of what I mean:

private static void Main(string[] args)
{
    var validPaths = LoadPaths(@"c:\dev\paths.txt");
    foreach (var path in validPaths)
    {
        Console.WriteLine(path);
    }
    Console.ReadKey();
}

private static List<string> LoadPaths(string filePath)
{
    var lines = File.ReadAllLines(filePath);
    var validPaths = new List<string>();
    foreach (var line in lines)
    {
        var tokens = line.Split('|');
        var firstPart = GetSpecialPath(tokens[0]);
        var path = Path.Combine(firstPart, tokens[1]);
        if (Directory.Exists(path))
            validPaths.Add(path);
    }
    return validPaths;
}



This is just a simple example that prints to the console, but you can build on it. Make methods for loading, then bind the result of those methods to your textboxes/listboxes.



I do want to clarify something: deleting directories isn't the best way to uninstall software; nor is it a great way to guarantee removal of spyware. Unless this is just something to automate a process you already follow, I suggest leaving anti-virus/-malware to professionals who deal in that kind of thing. It's hard enough for them with their signature-based detection rules.

Generally there's two things that you should almost never bother rolling your own: crypto and AV. You'll just end up with a weaker product than is already freely available.
Was This Post Helpful? 2
  • +
  • -

#7 TechCP  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 14-December 13

Re: I need to import from a textbox to listbox but adding a variable. [?]

Posted 19 December 2013 - 02:44 PM

I understand. This is just to automate a process I already follow.

Basically, after software is removed they leave behind remnants of the program that can sometimes lead to the programs re-installing themselves.
This is to rid the computer of the remnants.

But also you have to take into consideration the programs that don't list themselves in the Add/Remove programs list.

Normally I'll just run HijackThis and find what I want to remove there but this just gives me a broad general cleaning first.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1