How to get folder path for ClickOnce application

前端 未结 7 1716
Happy的楠姐
Happy的楠姐 2020-11-29 16:45

I need to write a file in the same folder where a console ClickOnce .application (executable file) resides. The folder where it launches from.

I tried u

7条回答
  •  庸人自扰
    2020-11-29 17:12

    I'm using Assembly.GetExecutingAssembly().Location to get the path to a ClickOnce deployed application in .Net 4.5.1.

    However, you shouldn't write to any folder where your application is deployed to ever, regardless of deployment method (xcopy, ClickOnce, InstallShield, anything) because those are usually read only for applications, especially in newer Windows versions and server environments.

    An app must always write to the folders reserved for such purposes. You can get the folders you need starting from Environment.SpecialFolder Enumeration. The MSDN page explains what each folder is for: http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx

    I.e. for data, logs and other files one can use ApplicationData (roaming), LocalApplicationData (local) or CommonApplicationData. For temporary files use Path.GetTempPath or Path.GetTempFileName.

    The above work on servers and desktops too.

    EDIT: Assembly.GetExecutingAssembly() is called in main executable.

提交回复
热议问题