I'm trying to create a console uninstaller for my friends, since they use addons for left 4 dead, and I am trying to make this to remove them before they update.. heres the code so far

using System;
using System.IO;

class Test
{

public static void Main()
{
// Specify the directories you want to manipulate.

try
{
// Determine whether the File exists.
{
// Delete File
}

// Determine whether the directory exists.
{
// Delete Directory
}

// Determine whether the directory exists.
{
// Delete Directory
}

// Determine whether the File exists.
{
// Delete File
}

// Determine whether the File exists.
{
// Delete File
}

// Determine whether the File exists.
{
// Delete File
}

// Determine whether the File exists.
{
// Delete File
}

// Determine whether the File exists.
{
// Delete File
}

// Determine whether the File exists.
{
// Delete File
}

// Determine whether the File exists.
{
// Delete Directory
}

// Determine whether the File exists.
{
// Delete File
}

// Determine whether the File exists.
{
// Delete File
}

// Determine whether the File exists.
{
// Delete File
}

// Determine whether the directory exists.
{
// Delete Directory
}

// Determine whether the directory exists.
{
// Delete Directory
}

// Determine whether the File exists.
{
// Delete File
}

// Determine whether the File exists.
{
// Delete File
}

// Determine whether the File exists.
{
// Delete File
}

// Determine whether the File exists.
{
// Delete File
}

// Determine whether the File exists.
{
// Delete File
}

// Determine whether the File exists.
{
// Delete File
}

// Determine whether the File exists.
{
// Delete File
}

// Determine whether the File exists.
{
// Delete File
}

// Determine whether the File exists.
{
// Delete File
}

// This will succeed because subdirectories are being deleted.
Console.WriteLine("The Delete operation was successful.");

// This will succeed because subdirectories are being deleted.
Console.WriteLine("The Delete operation was successful.");

// This will succeed because subdirectories are being deleted.
Console.WriteLine("The Delete operation was successful.");

// This will succeed because subdirectories are being deleted.
Console.WriteLine("The Delete operation was successful.");

// This will succeed because subdirectories are being deleted.
Console.WriteLine("The Delete operation was successful.");

// This will succeed because subdirectories are being deleted.
Console.WriteLine("The Delete operation was successful.");

// This will succeed because subdirectories are being deleted.
Console.WriteLine("The Delete operation was successful.");

// This will succeed because subdirectories are being deleted.
Console.WriteLine("The Delete operation was successful.");

// This will succeed because subdirectories are being deleted.
Console.WriteLine("The Delete operation was successful.");

// This will succeed because subdirectories are being deleted.
Console.WriteLine("The Delete operation was successful.");

// This will succeed because subdirectories are being deleted.
Console.WriteLine("The Delete operation was successful.");

// This will succeed because subdirectories are being deleted.
Console.WriteLine("The Delete operation was successful.");

// This will succeed because subdirectories are being deleted.
Console.WriteLine("The Delete operation was successful.");

// This will succeed because subdirectories are being deleted.
Console.WriteLine("The Delete operation was successful.");

// This will succeed because subdirectories are being deleted.
Console.WriteLine("The Delete operation was successful.");

// This will succeed because subdirectories are being deleted.
Console.WriteLine("The Delete operation was successful.");

// This will succeed because subdirectories are being deleted.
Console.WriteLine("The Delete operation was successful.");

// This will succeed because subdirectories are being deleted.
Console.WriteLine("The Delete operation was successful.");

// This will succeed because subdirectories are being deleted.
Console.WriteLine("The Delete operation was successful.");

// This will succeed because subdirectories are being deleted.
Console.WriteLine("The Delete operation was successful.");

// This will succeed because subdirectories are being deleted.
Console.WriteLine("The Delete operation was successful.");

// This will succeed because subdirectories are being deleted.
Console.WriteLine("The Delete operation was successful.");

// This will succeed because subdirectories are being deleted.
Console.WriteLine("The Delete operation was successful.");

// This will succeed because subdirectories are being deleted.
Console.WriteLine("The Delete operation was successful.");

}
catch (Exception e)
{
Console.WriteLine("What's this bullshit? a bug, you may need to retry manually: {0}", e.ToString());
}
finally {}
}
}

now the problem is, it wont let me use 'path' multiple times, and... it says i need an overload 2+ or something for file.delete

2
Contributors
8
Replies
9
Views
7 Years
Discussion Span
Last Post by Momerath

File.Delete only takes 1 parameter and you've supplied two. Get rid of all the 'true's.

awesome, that part worked, thanks. but what about

string path = @"C:\..."

it wont let me use 'path' its saying "a local variable named path is already defined in this scope

Well, that's because it is. You can use path again, you just can't define it again. So while you have

string path = @"blah...";
string path = @"blah...";

you would want

string path = @"blah";
path = @"blah"

Do note that you really can't change it the way you are doing it (all the lines up front) because it will only have the last value you set it to when you are done. You could do this

string path = @"blah 1";
Console.WriteLine("Deleting {0}", path);
File.Delete(path);

path = @"blah 2";
Console.WriteLine("Deleting {0}", path);
File.Delete(path);

etc

Or, even better would be

string[] paths = { @"path 1",
@"path2",
@"path3" };

foreach path in paths {
Console.WriteLine("Doing something with {0}", path);
File.Delete(path);
}

The last sample there means you'll only have one delete, you just use it over and over again.

ok, i made some changes.. but now im getting a green line saying "the variable 'path' is assigned but its value is never used"

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

namespace Beta_2
{
class Program
{
public static void Main(string[] args)
{

}
}
}

OK. I fixed this (again), but now.. will I need to add in an exception? for if it cant be found on "C:\Program Files (x86)\...." and tell it to go to "E:\Program Files (x86)\...." or will it skip it?

edit: ok, well now I need to make an exception for like "if not found at C:\, go to E:\" so it doesn't crash.. aswell as an exception IF the files are already deleted, then it close without it halting and windows saying not responding.. I also need an exception to stop if found on C:\ and not on E:\

like: you use it on C:... it deletes it from C:...., it looks on E:.... does not find it on E...., it closes without CRASHING, and vise versa, installed on E:... deletes from E:, looks on C:.. doesn't find it and closes.

Edited by xZombeast: n/a

You still aren't using the path variable, so why are you creating it? Change your code (for one example) to

string path = @"C:\Program Files (x86)\Steam\steamapps\common\left 4 dead\left4dead\cfg\autoexec.cfg";
Console.WriteLine("Deleting {0}", path);
File.Delete(path);

File.Delete does not throw an exception if it doesn't find the file, so you'll need to use File.Exists to check.

And you really should use an array, it will shorten your code dramatically:

using System;
using System.IO;

namespace vG_Uninstaller {
class Program {
public static void Main(string[] args) {
}; // You'd add the rest of the files without the drive letter
string[] drives = {"C:", "D:", "E:"};

foreach (String file in files) {
foreach (String drive in drives) {
if (File.Exists(drive + file)) {
Console.WriteLine("Deleting {0}", drive + file);
try {
File.Delete(drive + file);
} catch (IOException e) {
Console.WriteLine("Delete failed because some other software is using the file");
} catch (UnauthorizedAccessException e) {
Console.WriteLine("Delete failed because: User doesn't have permissions to delete the file or the file is read only");
}
break;
}
}
}
}
}
}

alright, i'll try that.. I mean it's working and everything, even with the 'string path' in there, just need to correct the unhanded exceptions now

edit: so you are saying where you have (path) it should say path and not the file path? i only get the green line where 'string path =' is

Edited by xZombeast: n/a

edit: so you are saying where you have (path) it should say path and not the file path? i only get the green line where 'string path =' is

Correct, it's telling you that you aren't using it, just assigning a value.