简体   繁体   中英

C# WriteAllBytes() in the program path?

i have excel file Embedded Resource, How can I create excel on Application.StartupPath?

public void openExcel()
    {

        //string tempPath = Path.GetTempFileName();

        string tempPath = Application.StartupPath; //ERROR is denied

        
        File.WriteAllBytes(tempPath, Properties.Resources.myexcel);

        Excel.Application excelApplication = new Excel.Application();
        Excel._Workbook excelWorkbook;
        excelWorkbook = excelApplication.Workbooks.Open(tempPath);

        excelApplication.Visible = true;

    }

Application.StartupPath is the path containing the executable file started.

If you want a temporary filename in the same directory, do

string tempPath = Path.Combine(Application.StartupPath, "temp.xls");

or similar.

However, using a temporary directory, as your commented code shows, would be better. There's no guarantee you can write to the executable's directory.

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