从服务器端文件夹中检索所有文件

时间:2022-04-23 11:13:07

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:

我有以下c#方法检索文件夹中的所有文件,并在asp.net应用程序中使用,并通过JavaScript进行AJAX调用:

    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.

我试过寻找达到目标的方法,但是我什么也没找到。也许我在搜索中没有使用正确的关键字,所以即使是关键字提示也会非常感谢。

3 个解决方案

#1


3  

You can simplify this using the following code.

您可以使用以下代码来简化这个过程。

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

#2


0  

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.

如果函数调用“oGetSoundFilesJAXHandler”。调用“正在服务器上执行,然后它将能够获取详细信息。但是如果这个函数在本地机器上运行,那么它就不能工作。

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.

要实现这一点,您希望在服务器*问的文件夹应该与您共享,并且应该提供使用服务器ip地址的路径来访问它。

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

这只是您的客户端访问服务器文件夹的权限问题。

#3


0  

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.

由于文件夹没有更改,我将它直接添加到方法中,但也可以像字符串一样轻松地将其传递给该方法。

#1


3  

You can simplify this using the following code.

您可以使用以下代码来简化这个过程。

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

#2


0  

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.

如果函数调用“oGetSoundFilesJAXHandler”。调用“正在服务器上执行,然后它将能够获取详细信息。但是如果这个函数在本地机器上运行,那么它就不能工作。

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.

要实现这一点,您希望在服务器*问的文件夹应该与您共享,并且应该提供使用服务器ip地址的路径来访问它。

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

这只是您的客户端访问服务器文件夹的权限问题。

#3


0  

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.

由于文件夹没有更改,我将它直接添加到方法中,但也可以像字符串一样轻松地将其传递给该方法。