In VStudio 2019, VB, .NET-5: for a ClickOnce installed app, how do I locate the application's data directory at runtime? - vb.net

My VStudio is 2019 Community, the application is an old VB Windows/Forms app being brought forward from .Net-3.5-ish to .Net-5.
Under .Net-4.8 or earlier, using the namespace System.Deployment.Application, one could access ApplicationDeployment.DataDirectory to locate that artificially-generated directory in \Users\Username\AppData\Local\Apps\2.0\... that a 1-click installation would create and populate with data files from the Project Build/Publish.
Under .Net-5, I believe that particular set of functions has been taken out - yet the ClickOnce installer still places Data files there. How do I programmatically locate that directory? The Application Path can be located from System.AppContext.BaseDirectory, but that doesn't help in locating the data.
This app, an old bit of VB code, has a large number of application data files that need to be installed along with the code. As there are more than 40 of them, we've kept them in their own \Data directory, which is included in the Project, is under Git control to keep their versions aligned with their code...
In an uninstalled app, we could keep this \Data directory adjacent to the executable, and find it easily at runtime: but with a ClickOnce installed app, that falls apart. In the Publish configuration, all the data files are identified, with Group=Required, Publish Status=DataFile.
Can I either:
Easily configure the 1-click installer to place our \Data directory and its contents into one of the standard \Local\Appname or \Roaming\Appname directories that seem to also get created, but left empty? Those can easily be located at runtime.
Easily construct at runtime the path to that obscure data directory that 1-click normally creates and uses for data?
Cheers, and Thanks! Bob

Related

Deploy problem with project with an access database integrated

I´ve been having problems with my project and the database it has. I have created a simple project and I created an access database in it, the database is in the project folder. This is the connection string specified in the App.config:
<connectionStrings>
<add name="Project.My.MySettings.BaseDeDatosConnectionString"
connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Recursos\Datos\BaseDeDatos.accdb"
providerName="System.Data.OleDb" />
</connectionStrings>
But when I publish my project, install it in my computer and executed it I get the error:
'C:\Users\USER_NAME\AppData\Local\Apps\2.0\Data\6QPXHXGQ.ZM0\MVT91HXD.V1B\simu..tion_0000000000000000_0001.0000_49edcdec2714f7aa\Data\Resources\Data\Database.accdb' is not a valid path . Make sure that the path is spelled correctly and that you are connected to the server where the file is located.
And of course the program remains useless. I´ve checked the folder and indeed there is no data base in there, I didn't even knew it would create a file there (I know nothing related to databases and deployment, I am just following tutorials). I was using a SQL data base before and it worked fine after installation, but I had to change it. What could I be missing?
I am using VS 2015 in spanish and VB.NET.
If you are using the Publish functionality in VS then you are using ClickOnce. ClickOnce apps have a dedicated folder to which "|DataDirectory|" resolves at run time. It is NOT the program folder. If you want to use "|DataDirectory|" in your connection string then you have to specify that the data file is indeed a data file via the properties, so that it will be placed in that folder. I don't really use ClickOnce but I believe that, to do that, you need to open the Publish page of the project properties, click the Application Files button and then set the Publish Status of that file to Data File. I'm not sure whether the subfolders for the source file will be honoured after publishing or not but you can test that.
Note that, if you simply copy the application files to the target machine or use some installer technology other than ClickOnce then "|DataDirectory|" will resolve to the program folder. For the record, it resolves to the App_Data folder for ASP.NET apps (possibly only Web Forms, not sure).

How can I get to the root folder of another project in a .NET Core solution?

I need to read the settings file (appsettings.json) from another project in my solution. When I use:
Directory.GetCurrentDirectory()
From within the current project, I get the following path:
{projectRootFolder}\bin\Debug\netcoreapp3.0\
My question is: How can I get to the exact same folder in another project in the same solution? Or is there a better way to access the settings file from another project within the current solution?
If I understand the problem correctly there are two misconceptions:
It has little sense to access output directory of an another project as the structure has sense in compile time only. You will not have the same structure in run-time once the application is "published".
The Directory.GetCurrentDirectory() returns the current working directory. It is just a coincidence to be set to project output directory by Visual Studio. It can be totally different directory.
It is not clear to me what exactly you are trying to achieve. I recommend using the configuration system provided by .net core to access the configuration and add that other appsettings.json as another configuration provider.
If you really need to open the settings file then the project with the settings file (A) should mark the file as "Copy to Output Directory" and the project to open the file (B) should reference the project A. So the settings file will be copied to output of the project A too.
What you're attempting to do is not possible. There's no inherent way for ASP.NET Core to know where a totally different app running in a totally different process is located.
If you need to access appsettings.json from another project, then you would need to include it as a linked file in your project, and set it to copy to output. Then, you're accessing it actually from your project (which is all you can do), but the file itself is shared.
However, this is almost always a bad idea, and usually a sign that you're doing something wrong. If you truly do need to share the settings, then what you should be doing is putting them in a distributed config provider like Azure Key Vault or similar, where both projects can independently access the settings from a common store.

Common wwwroot folder for multiple websites in MVC 6

I have multiple websites which use /wwwroot/assets folder (html theme, css and javascript files) to load the static content.
Currently I am copying assets folder in each site. All of my projects are sitting under a common parent directory.
I don't want to copy the /wwwroot/assets folder into each website. Is there a way to share one assets folder between all sites. May be by providing a direct file system path or something?
At the moment it's not clear from the documentation what sorts of values the webroot key in the project.json file will accept, but so far it would appear that Visual Studio doesn't care for very complicated paths. For example, setting the value to ../wwwroot causes the entry to disappear in the Solution Explorer.
If you look at the kpm code that bundles your project up for deployment, it appears to combine your project's directory with whatever is stored in the wwwroot key, so even though Visual Studio may not understand it, relative paths appear to be supported. Using kpm bundle from the command line confirms this, and a directory above src bundles correctly when using a relative path.
Depending on your particular needs, there is one way that should work that makes kpm and Visual Studio happy, but it will depend on your build environment as to whether that is a good option for you.
Windows, OSX, and Linux all support creating symbolic links for directories, which would allow you to have your assets directory in one location in the filesystem and then create links to it elsewhere. For example, if you had assets in /projects/shared/assets, you could create a link in both of your other projects (e.g. /projects/project1/src/wwwroot/assets) that point to the "real" location.
In Windows, the command would might something like this
mklink /j "C:\link\to\create" "C:\path\to\assets"
So if you did
mklink /j "C:\source\shared\assets" "C:\source\project1\src\wwwroot\assets"
project1 would appear to have an assets directory inside of wwwroot and the build process would be happy since it would appear to each project that the files were local. One thing to note here is that Windows supports a number of different sorts of links. /j specifically creates a junction rather than a true symbolic link. The differences are a bit subtle, but this is a good description of the differences. It is enough to know that if you're working locally, the /j command doesn't require administrative rights and Visual Studio and kpm will both be happy.
In OSX and Linux, the command is similar:
ln -s /link/to/create /path/to/assets
and like Windows, they support different sorts of links.
In any case, under the right circumstances, this might work well without needing any special support from the new ASP.NET project structure, but it would be nice to eventually have that as well.

How do I publish specific files not in project in VS 2010?

Is there a way I can publish *.ascx files without adding them to my project?
I am trying to make my user controls in Visual Studio 2010 reusable. I have a project containing my user controls called ControlsLibrary solution directory. I copy the ascx files to the web directory upon building the project with build events.
I use the command copy "$(SolutionDir)ControlLibrary\*.ascx" "$(ProjectDir)controls\"
This copies the *.ascx files from G:/SolutionDirectory/ControlsLibrary to C:/Inetpub/wwwroot/WebProject/controls
Now when I publish WebProject, the publish does not copy the *.ascx files from C:/Inetpub/wwwroot/WebProject/controls to my website. This is because they are not added to my project.
Is there a way I can publish *.ascx files without adding them to my project?
I know there is a way to publish all files by selecting the option "All files in this project." That includes more files than I want.
Have you considered using the Virtual Path Provider approach? Here's a good article on it: Load Web Forms and User Controls from Embedded Resources.

Which files are used by a program?

I have written a program on Visual Basic. In the debug folder, there are many files:
Database1.mdf
Database1_log.ldf
MyData.Designer.vb
MyData.xsc
MyData.xsd
MyData.xss
WindowsApplication1.exe
WindowsApplication1.config
WindowsApplication1.pdb
WindowsApplication1.vshost
WindowsApplication1.vshost.exe
WindowsApplication1.vshost.exe.manifest
WindowsApplication1.xml
I want to publish my program. Are all of those files necessary for the program? Which of them are used for my database?
Because I want to put a button in my program that backs up the database. Which files must be backed up?
First of all, you should publish the Release version of your software, not the debug version so the files will be a bit different. As for which files to publish, if you use the Setup project you will be able to select the files based upon what your application needs. For example, it looks like you are including database files with your application (Database1.mdf and Database1_log.ldf). You could add these files to the setup project.
The setup project will know to include your exe and your config file (unless you tell it not to) so you will be covered there. Here is a video and a written walkthrough of how to create a Setup project:
http://msdn.microsoft.com/en-us/library/ms241903.aspx
http://www.youtube.com/watch?v=Lcue0jo41AM
As for your PDB files, these are the Program Database Files that are used for debugging (and should never be give to the customer/end user).
http://msdn.microsoft.com/en-us/library/ms241903.aspx
As for backing up your database, back up the MDF and LDF files.
No, all of the files above are from your debug compile output. You can change what is output by changing your build configuration. Go to Build, Configuration Manager and switch to Release. It's also on the toolbar.
In general your ProjectName.exe (but not the .vshost.exe), .config (but not the .vshost.exe.config) and MDF/LDF files are needed for publishing. You also have an XSD File which will also be needed.
The MDF/LDF files are your database.