C# 创建到已知文件夹的相对路径

C# 创建到已知文件夹的相对路径,c#,file,path,directory,C#,File,Path,Directory,我正在寻找一种从相对文件路径生成文件列表的方法 到目前为止,我已经想到了这个: string[] filePaths = Directory.GetFiles(@"~\Images\Uploaded\"); 但是我得到一个错误,说路径不存在,尽管它确实存在。如果它是ASP.Net应用程序,您可以使用Server.MapPath将相对路径转换为绝对路径 string folderPath = Server.MapPath("~\Images\Uploaded\"); 从 您可以使用获取当前目录

我正在寻找一种从相对文件路径生成文件列表的方法

到目前为止,我已经想到了这个:

string[] filePaths = Directory.GetFiles(@"~\Images\Uploaded\");

但是我得到一个错误,说路径不存在,尽管它确实存在。

如果它是ASP.Net应用程序,您可以使用Server.MapPath将相对路径转换为绝对路径

string folderPath = Server.MapPath("~\Images\Uploaded\");

您可以使用获取当前目录,并与相对路径组合以形成绝对路径,并从此绝对路径获取文件

试着跟随

string[] filePaths = Directory.GetFiles(Path.Combine(Directory.GetCurrentDirectory(),@"\Images\Uploaded\"));
或者只使用相对路径而不使用
~

string[] filePaths = Directory.GetFiles(@"\Images\Uploaded\"));

我自己没有试过,但我对
Directory.GetFiles(VirtualPathUtility.GetDirectory(@“~\Images\upload\”)很好奇