views:

3580

answers:

7

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());
            }
        }
    }
A: 

I guess the first question is: Does it work if you use a local path instead?

Kyralessa
A: 

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.

Ely
+3  A: 

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.

Travis Collins
+2  A: 

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

Brett McCann
A: 

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.

Rorschach
I'm using a remote path (\\server\folderx\foldery)These files are stored on one of our servers and my program is very specific. I only want it to work on certain directories and all the directories have the same format.
rodey
+1  A: 

Follow up:

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

This site is great!

rodey
A: 

I am using the same code for deleting files from one of my project server. But the above code , instead of trying to get iles from \server\folder , it is trying to connect to my local system C:\server\folder. Any idea how to solve this issue?

You should start a new post to ask a new question, instead of resurrecting a year-old question and adding your own question in as an answer. But it looks like your problem is that you missed a slash. A remote share is addressed as `\\server\share\folder\file.ext`. A single leading slash refers to the root directory of the drive that holds your current working directory.
P Daddy