Can I write a file to a folder on a server machine from a Web API app running on it?
Asked Answered
S

2

10

I have this code in my Web API app to write to a CSV file:

private void SaveToCSV(InventoryItem invItem, string dbContext)
{
    string csvHeader = "id,pack_size,description,vendor_id,department,subdepartment,unit_cost,unit_list,open_qty,UPC_code,UPC_pack_size,vendor_item,crv_id";

    int dbContextAsInt = 0;
    int.TryParse(dbContext, out dbContextAsInt);
    string csvFilename = string.Format("Platypus{0}.csv", dbContextAsInt);

    string csv = string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12}", invItem.ID,
        invItem.pksize, invItem.Description, invItem.vendor_id, invItem.dept, invItem.subdept, invItem.UnitCost,
        invItem.UnitList, invItem.OpenQty, invItem.UPC, invItem.upc_pack_size, invItem.vendor_item, invItem.crv_id);

    string existingContents;
    using (StreamReader sr = new StreamReader(csvFilename))
    {
        existingContents = sr.ReadToEnd();
    }

    using (StreamWriter writetext = File.AppendText(csvFilename))
    {
        if (!existingContents.Contains(csvHeader))
        {
            writetext.WriteLine(csvHeader);
        }
        writetext.WriteLine(csv);
    }
}

On the dev machine, the csv file is saved to "C:\Program Files (x86)\IIS Express" by default. In preparation for when it is deployed to its final resting/working place, what do I need to do to get the file to save, e.g., to the server's "Platypi" folder - anything special? Do I have to specifically set certain folder persimmons to allow writing to "Platypi."

Is it simply a matter of changing this line:

string csvFilename = string.Format("Platypus{0}.csv", dbContextAsInt);

...to this:

string csvFilename = string.Format(@"\Platypi\Platypus{0}.csv", dbContextAsInt);

?

Seigneur answered 7/2, 2014 at 23:9 Comment(0)
O
19

In the case of the IIS folder the application has rights to write in there. I suggest to write files to the App_Data folder.

When you want to save files outside the IIS application folder you have to give the service account of the IIS application pool (I think it by default is NETWORKSERVICE) the appropriate rights on that folder.

As requested by B. Clay Shannon my implementation:

string fullSavePath = HttpContext.Current.Server.MapPath(string.Format("~/App_Data/Platypus{0}.csv", dbContextAsInt));
Ovate answered 7/2, 2014 at 23:13 Comment(10)
Would I write to the AppData folder via Environment.SpecialFolder.ApplicationData?Seigneur
The following line simply returns the string "ApplicationPath": string pathOfMostResistence = Environment.SpecialFolder.ApplicationData.ToString();Seigneur
Simply use Server.MapPath("~/App_Data/..."). This returns the app_data folder. The ~ means application root folder.Ovate
Do you need more help?Ovate
Trying to see just what this would return, I added this code: string bla = Server.MapPath("~/App_Data/"); ...and "Server" is not recognized ("Cannot resolve symbol 'Server'")Seigneur
Try HttpContext.Current.Server.MapPathOvate
I am referencing System.Web, I do have a "using System.Web" and even prepending HttpServerUtility fails; in fact, Intellisense only shows UrlTokenEnclode and UrlTokenDecode as legitimate selections after "HttpServerUtility."Seigneur
Don't use HttpServerUtility but HttpContextOvate
OK, that works fine; add it as an answer, and I'll mark it as such. I'm going to add my own answer, with the exact code I used, too. Or, edit your existing answer, I should say, with this additional info.Seigneur
@B.ClayShannon: I took your code and updated it a little to my liking.Ovate
S
4

Thanks to Patrick Hofman; this is the exact code I am using, and it is saved to the project's App_Data folder:

public static string appDataFolder = HttpContext.Current.Server.MapPath("~/App_Data/");
. . .
string csvFilename = string.Format("Platypus{0}.csv", dbContextAsInt);
string fullSavePath = string.Format("{0}{1}", appDataFolder, csvFilename);
Seigneur answered 10/2, 2014 at 17:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.