简体   繁体   中英

How to save a file in a changing location c#

I have a report that I created in Excel using c#. Every user will be downloading it form their individual computers. There for, making the saving and opening operation of my program difficult. I want to be able to save it to the computers documents location. Here is what I have as far as relavent code goes.

string subPath = "Documents/Reports";

bool isExists = Directory.Exists(Server.MapPath(subPath));

if (!isExists)
        Directory.CreateDirectory(Server.MapPath(subPath));

xlWorkBook.SaveAs("Reports\\" + filename, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();

Any ideas on how to create a path that will always work with any pc?

You can resolve the machine's local documents folder path using the Enironment.GetFolderPath method:

string subPath = 
    Environment.GetFolderPath(
        Environment.SpecialFolder.MyDocuments) + "\\Reports";

您可以使用Environment.GetFolderPath方法并将其Combine为真实路径:

    String subPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),"Reports");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM