Alex
Alex

Reputation: 3998

Retrieve all files from a server side folder

I have the following C# method that retrieves all the files in a folder, and is used in an asp.net application and called by making an AJAX call through JavaScript:

    public string GetSoundFile(string pSoundFolder)
    {
        string[] pFiles = Directory.GetFiles(pSoundFolder);

        string pFileList = "";

        for (int ii = 0; ii < pFiles.Length; ii++)
        {
            if (pFileList == "")
            {
                pFileList = pFiles[ii];
            }
            else
            {
                pFileList += "|" + pFiles[ii];
            }
        }

        return (pFileList);
    }

and is called by doing the following:

oGetSoundFilesJAXHandler.call("C:\\Projects\\");

From what I understand, the line

string[] pFiles = Directory.GetFiles(pSoundFolder);

is used for local files?

The application will be run on the client side and will need to access a server side folder. If I am correct, then my method cannot be adapted to perform the task I need it to.

I have tried:

        oGetSoundFilesJAXHandler.call("~//Projects//");

But this does not return the file list.

I have tried searching for a way of achieving my target, but I have not been able to find anything. Maybe I am not using the right keywords in my search, so even keyword hints would be greatly appreciated.

Upvotes: 3

Views: 11845

Answers (3)

Richard Friend
Richard Friend

Reputation: 16018

You can simplify this using the following code.

public string GetSoundFile()
{
    var files = Directory.GetFiles(HttpContext.Current.Server.MapPath("~/sounds"));
    return String.Join("|",files);
}

Upvotes: 5

Alex
Alex

Reputation: 3998

I found the answer myself.

I needed to use

 public string GetSoundFile()
    {
        string WorkingDirectory = HttpContext.Current.Server.MapPath("~/sounds");                    

        string[] pFiles = Directory.GetFiles(WorkingDirectory);

        string pFileList = "";

        for (int ii = 0; ii < pFiles.Length; ii++)
        {
            if (pFileList == "")
            {
                pFileList = pFiles[ii];
            }
            else
            {
                pFileList += "|" + pFiles[ii];
            }
        }

        return (pFileList);
    }

I needed to use:

HttpContext.Current.Server.MapPath("~/sounds");

To pass in the folder correctly.

As the folder does not change I added it directly to the method, but could just as easily have passed it in as a string.

Upvotes: 0

Ashutosh Singh
Ashutosh Singh

Reputation: 662

If your function call "oGetSoundFilesJAXHandler.call" is executing on the server then it will be able to fetch the details. But if this function is running on your local machine then this will not work.

To achieve this the folder you want to access on server should be share with you and its path using server ip address should be provided in order to access it.

Its just a matter of access rights for your client to the server folders.

Upvotes: 0

Related Questions