How to add file into Appx directory - c#

I've 2 folders visible in Solution Explorer in my project (sample grid app):
fasdalocation and fasadalocation2.
fasadalocation was previously oryginal DataModel folder in sample grid app, fasadalocation2 is folder created by me.
Two folders contain identical file: SampleData.json.
I can acces to json file from fasadalocation by
Uri("ms-appx:///fasadalocation/SampleData.json")
unfortunatelly this don't work for fasadalocation2 (I got FileNotFoundException).
It's caused by fact that
C:\Users\Jakub\Documents\Visual Studio 2013\Projects\sample grid app\sample grid app\bin\Debug\AppX
location doesn't contain fasadalocation2.
How can I add fasadalocation2 into appx?
Of course I thought that simple insertion folder into AppX location isn't a gentle solution...

It looks like the 2 files are content files that need to be deployed to your output folder and one of them does not have the correct build action to copy it to the folder.
Right click on SampleData.json in the fasadalocation2 folder and ensure it's build action is set to Content and CopyAlways.

Related

how to set image in Visual Studio C# Windows Forms with Image.FromFile

So I'm trying to set a custom image for a form application I've made. The images I'm trying to target are in a folder called "Images" on the same level as my Solution file. The solution file is a C# windows forms (net core framework) solution. It's a basic form app that I want to display an image based on a users selection, however right now I get an unhandled exception everytime I try to set the image with this code:
picFood.Image = Image.FromFile("../../Images/burger.jpg");
The exact error is "System.IO.FileNotFoundException: ../../Images/burger.jpg"
In another totally unrelated solution this works. Folder structure is the same. A folder called Images, on the same directory level as the .sln file holds the images there. They're in my solution explorer and everything. I've tried this with one "../" and no "../" as well so I'm not sure what to do from here.
Files with relative paths are opened relative to the working directory of your application.
In this case, when launching from within Visual Studio, the default is the bin folder where the compiled application is put by default.
So if your binary is in <project dir>/bin/Debug/App.exe this path will resolve to <project dir>/Image/burger.jpg.
If you have changed something in your build configuration, or your application switches directory at runtime (e.g. via Directory.SetCurrentDirectory), this path may be different than you expect.
To understand your issue, I suggest you start looking at what your working directory is. You can obtain that in your code via Directory.GetCurrentDirectory().
You can also resolve your relative path using Path.GetFullPath.
Print these two values to see where your program attempts to load the file from.
Keep in mind that any image files you put in the solution/project folder will need to be copied with your binary if you want to use them.
To use relative paths without .. you can copy them alongside your binary during compilation, see:
VS2010 How to include files in project, to copy them to build output directory automatically during build or publish and Copying Visual Studio project file(s) to output directory during build for how to do that.

Changing output path of XAML files in VIsual Studio 2015 Universal App

I have a VCXProj file for a Universal Windows app that compiles some xaml files
<Page Include="..\View\ViewModel.xaml" />
One result of this is the xaml file is deployed as part of the package as a resource: ms-appx:///AppNamespace/ViewModel.xaml
If the file path was instead "View\ViewModel.xaml" then the resource path would also be "AppNamespace/View/ViewModel.xaml"
Unfortunately, since it is in the parent directory, it gets flattened and goes in the root. I want a way to specify the output path, rather than let the MSBuild system preserve it, something like
<Page Include="..\View\ViewModel.xaml">
<TargetPath>View\MyViewModel.xaml</TargetPath>
</Page>
But I cannot for the life of me find a way to do this. Is there anything out there?
I think this is by design, I couldn't find an option in VS tool that can specify the output path of a xaml file.
By default if you create a View folder in your project from VS, and create a new Page in this View folder, the VS compiler will automatically extract this XAML file out of the View folder and put it in the root folder of your project folder. And even you created one folder in the project from VS, this folder will not be created actually in your project physical folder.
For example, I created a folder named "Test" from VS, and I copied an image into this folder. The path of this image is actually like this:<Image Include="C:\Users\(pc-account)\Pictures\1.jpeg" />, and you will not find the "Test" folder in the physical path of this project. Now if we create a folder also named "Test" in the physical path, not only from VS, and copy another image into this folder, open this project from you VS, right click the "Test" folder, by adding existing item to select the second image into this "Test" folder, then you can see from VS:
And in the Appx folder of your project it looks like this:
As you can see, the first image which is added to the project from VS is extracted from the Test folder to the root folder, and the second image which is manfully added to the project is still in the Test folder.
You can see the path of this two images in the VCXProj file like this:
I'm afraid there is no options in VS tool that can automatically do this work, we can only
manually create a folder with the same name as one created from VS tool.
copy the .xaml, .xaml.cpp, and .xaml.h file into this manually-created folder.
add existing item from vs to this folder.
debug your project then you can see this View2 folder in the Debug folder.
in the meanwhile, in your VCXProj file you can see this xaml page's path:

Load XML file into application output file

I have a folder in my main project that is separate than the Resources folder but also has some resources it that I would like to copy to the output folder as I reference it throughout the app.
I have set the build action to Resource, content, embedded content.. tried them all..
Also set it to always copy to output directory.
Now, from within my application I'm entering
AppDomain.CurrentDomain.BaseDirectory + "Dashboard\\Role.xml";
or also
"..\\..\\Dashboard\\Role.xml";
for the path and get an exception that says that the file can not be found.
Both of these paths work in my development machine but not once I deploy through click once.
I have tried to add it to the application files in the publish section as suggested in another post but it does not appear there.
I have also tried to put it in the resources folder and still nothing.... any ideas?
I followed some steps in this link and got it working:
https://msdn.microsoft.com/en-us/library/kzy0fky2.aspx
Basically, you mark the file as build action "Content" and set it to always copy.
Then, you go to the application files in the publish section of your clickonce application and they will now show up.
Switch them from datafile to "Include" and you're set!

Visual Studio: different project folder structure for publishing/editing than the physical one

I'd like to add files to a web csproj that are located under its directory, but in a different directory structure than the physical one. Example physical directory structure:
MyProject.csproj
Assets
Settings.config
Now my project needs the following structure when I publish the project (i.e. on the server when deployed) and would be nice if this structure would be visible from Visual Studio too:
MyProject.csproj
Config
Settings.config
Notice that Settings.config isn't in the Assets but in the Config folder.
I don't want to copy the files over, I can't move them and I can't change where these files are loaded from.
Is it possible for such files to be located in the Assets folder while
from VS they appear to be in the Config folder,
and when the project is published they are copied to the Config folder.
The latter one is possible with post-build tasks I think and I believe I can implement it. However I'd like to have the whole development experience, including the folder structure in VS, to show the file being under Config. Is this possible?
I'm looking for something like solution folders, but for projects. Adding files as links would work great, but since the files are in the physical folder of the project I get the error "Cannot add a link to a file that is inside the project tree".

How to add external file outside the application folder to clickonce deployment (winappliction.net) [duplicate]

I've got an application that I'm moving over to ClickOnce and the app has a moderately sized data folder with hundreds of files that I need to inlcude in the deployment. The folder needs to be in the same place relative to the EXE after deployment. I've seen several suggestions on how to do this but there doesn't seem to be a agreed upon method for doing this.
Any suggestions would be great -
Thanks!
One good way of doing this is:
Create a folder under the app in VS name e.g. "datafiles"
Add all files to that folder using Add as link in the dialog box after selecting Add existing item on the folder
Mark all files as Copy if newer (Copy to output directory property)
Make sure the build action is content
--> when you publish the files will be put in that folder and be a part of the application installation
Good luck!
After deployment, all files marked as data are placed in the ApplicationDeployment.DataDirectory folder. I know of no way to change this. You could copy the data files during the first run of your app, but this approach will not survive any upgrades that include data file changes.
Alternatively if you have control over the location of the data folder during development, you can place in the same relative (to the app folder) location as will be specified after deployment.
https://msdn.microsoft.com/en-us/library/kzy0fky2.aspx
https://msdn.microsoft.com/en-us/library/6fehc36e.aspx
These two articles provide methods of doing this. Between the two of thing you can find one that works for you. The one that worked for me was:
With a project selected in Solution Explorer, on the Project menu,
click Properties. Click the Publish tab. Click the Application Files
button to open the Application Files dialog box. In the Application
Files dialog box, select the file that you wish to mark as data. In
the Publish Status field, select Data File from the drop-down list.

Categories

Resources