Question

I'm currently trying to create an image from a byte[] stream using Streamwriter. Here is my code so far:

foreach (var image in continent.Images)
      {
        var filename = Path.GetFileName(image.FileName);
        var newpath = Directory.GetCurrentDirectory() + filename;
        StreamWriter streamwriter = new StreamWriter(newpath);
        streamwriter.Write(image._Image);
        streamwriter.Close();
        streamwriter.Dispose();
      }

My only issue is that GetCurrentDirectory() returns:

C:\\Program Files (x86)\\IIS Express

As this is designed to run in the context of a web server I'm slightly confused as to how to reference the specific directory I require. My directory structure is as follows:

  --Root
    --media
     * Create image under this directory *
    --umbraco
     * Code is run under this directory * 

I simply want to say, create this new file under the media directory. I've tried using ../media/ as part of my path and i've also tried ..\media\ but neither seem to work. Any pointers would be greatly appreciated as I'm new to .net and IIS and usually deal with Apache servers using PHP.

Was it helpful?

Solution

You should use MapPath to navigate the directory hierarchy:

Path.Combine(Server.MapPath("~/media"), filename)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top