Pregunta

Estoy tratando de convertir "~ / uploads / images /" en una ruta absoluta que puedo crear un FileStream partir. He intentado VirtualPathUtility y Path.Combine pero nada parece que me diera el camino correcto. Lo más cerca que me dieron fue VirtualPathUtility.ToAppRelative, pero eso fue sólo la ubicación del archivo como un hijo directo de C:.

Tiene que haber una manera de hacer esto.

¿Fue útil?

Solución

Se busca la MapPath método.

// get the path in the local file system that corresponds to ~/Uploads/Images
string localPath = HttpContext.Current.Server.MapPath("~/Uploads/Images/");

Se usa junto con Path.Combine para crear una ruta de acceso:

string fileName = Path.Combine(
                      HttpContext.Current.Server.MapPath("~/Uploads/Images/"),
                      "filename.ext");
using (FileStream stream = File.OpenRead(fileName))
{
   // read the file
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top