Question

I'm trying to delete a directory that contains XML files from a remote computer. My code compiles and runs fine, but when I go to get a list of XML files in the path I specify, it is not returning anything. Am I missing something permission wise?

I have ran it from my computer logged on as myself and from another computer logged on as a different user. Both accounts have full control over the directory that contains the XML files.

I'm using .NET 2.0.

   static void Main(string[] args) {
        string directory, ext = ".xml"; // have tried xml and .xml

        if (args.Length != 1) {
             // do absolutely nothing if we do not exactly 1 argument
        } else {
            Console.WriteLine("Argument accepted.");
            directory = args[0];

            // make sure the directory passed is valid
            if (ValidateDirectory(directory)) {
                Console.WriteLine("Directory is valid.");
                DeleteFiles(directory, ext);
            }
        }
        Console.WriteLine("Done.");
    }

    static bool ValidateDirectory(string d) {
        return Regex.IsMatch(d, @""); // I removed my regex - it validates properly
    }

    static void DeleteFiles(string d, string ext) {
        DirectoryInfo di;
        FileInfo[] fi;

        di = new DirectoryInfo(d);
        fi = di.GetFiles(ext);

        Console.WriteLine("Number of files = " + fi.Length + ".");
        foreach (FileInfo f in fi) {
            try {
                Console.WriteLine(f.FullName);
                f.Delete();
            } catch (Exception ex) {
                // do nothing when there is an exception
                // just do not want it to quit
                Console.WriteLine(ex.ToString());
            }
        }
    }
Was it helpful?

Solution

I think you should be using *.xml instead of simply .xml. But I also concur with Kyralessa, test on your local machine first, then add in the complexity of going across a network.

OTHER TIPS

in DeleteFiles, you have the following line:

fi = di.GetFiles(ext);

where ext is the extension you pass in, which I believe is just '.xml'. Get files is looking for any files called '.xml'. GetFiles takes wildcards, which I believe is what you are intending to do. Put an asterisk (*) at the front and give that a try.

-Brett

Follow up:

I needed to use *.xml (should have known that!) and now it works.

This site is great!

I assume you are passing in a network path? Does it fail when you run the program on a local path? Does this line: fi = di.GetFiles(ext); Return any fileInfo objects?

You probably just have something small wrong that can be fixed by some debugging.

What are you passing in as the argument? Are you using a Mapped Drive or the direct reference (i.e. //server/folder)?

Instead of your ValidateDirectory, you should use Directory.Exists(directory) just to see if it can see the directory at all.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top