Load XML file into application output file - c#

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!

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.

Finding a specific file path when two files have the same name in different locations in C#

I'm trying to load and save an xml file called Modules.xml in my code. I have currently got the file path hardcoded as shown below. I am trying to get the file path within my code without it being hardcoded.
I have tried using Path.GetDirectoryName and new FileInfo("Modules.xml").Directory.FullName. However, both of these target the file in my debug folder, when the file I need is in the main solution folder.
Is there a way to target the file in my main solution folder instead of my debug folder? (both files are called Modules.xml)
doc.Save("C:\\Users\\Matthew\\Desktop\\Year4\\Object Oriented\\Project1\\Project1\\Modules.xml");
Both file locations are shown below:
C:\Users\Matthew\Desktop\Year4\Object Oriented\Project1\Project1\Modules.xml
^^^this is the file path I need for my code^^^
C:\Users\Matthew\Desktop\Year4\Object Oriented\Project1\Project1\bin\Debug\Modules.xml
The best approach here would be to use a configuration file, e.g. app.config, for storing such a string. Then you can change file path without recompiling the code, and your file can be stored in any location accessible by application.
If you really want to access your file the way you explained, AppDomain.CurrentDomain.BaseDirectory will provide you with the bin/Debug location in runtime. Then you can find a relative path from there like:
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, #"..\..\", fileName);
where fileName is "Modules.xml" for example.
I have tried using Path.GetDirectoryName and new
FileInfo("Modules.xml").Directory.FullName. However, both of these
target the file in my debug folder, when the file I need is in the
main solution folder.
That's because bin\Debug is your working directory when you start and run the project. To change that, you can set the working directory environment variable to point to your solution directory (instead of bin\debug|release) which I wouldn't recommend that. Because when you finally endup with development, and release the application, there wouldn't be any solution directory that holds your XML file. What I can suggest is to copy your XML file to the output folder. Either you are in development (debug) or production (release) mode, the XML always going to be copied to final directory. And you can access the working directory with something like AppDomain.CurrentDomain.BaseDirectory. To enabling copy XML to output directory, right-click on it, choose Properties, set Build Action to None, and set Copy to Output Directory to Copy Always or Copy if newer. You're good to go now.

c# have sound files in build

I need to make a game for school.
I was adding some sound effects and music.
I have the paths to the .wav in the class and it will play them, but only when I start the game from the bin map in the project. I figgered out that the files are not included inside off the build. I started looking for it but could not find the correct way for it.
I have:
MediaPlayer _musicPlayer = new MediaPlayer();
string s = System.IO.Packaging.PackUriHelper.UriSchemePack;
Uri uri = new Uri(#"pack://application:,,,/{Assembly name};component/Resources/Sounds/Music/Main_theme_-_Thiago_Adamo.wav")
_musicPlayer.Open(uri);
_musicPlayer.Play();
This however does not work. If I add an messagebox with:
uri.IsFile.ToString()
It displays false.
On the file I set the propeties to be:
Build Action: Resource
Copy To Output Directory: Copy Always
I hope someone can help me.
Add the files to the project in your solution in Visual Studio, so they show up in the Solution Explorer (docked on the right side of the VS window by default). Click on a file so you see the properties window for that file (shown below the Solution Explorer in VS by default). Make sure the Copy to Output Directory property is set to either Copy Always or Copy if newer, and the Build Action is set to Content. Do this for each sound file you need.
At this point, the files will be copied to same folder as your application when you build it. They will not be packaged. They'll just be there in the file system next to your program. If you build the application in Debug mode, this is in the /bin/Debug folder (by default) for your project. If it's Release mode, it's the /bin/Release folder, but as long as your working directory matches your application you can reference them in the Uri by file name alone (otherwise you'll need to check the application path and build the full path to the file).
The reason you need Content instead of Resource is in the Remarks section of the MediaPlayer class documentation:
When distributing media with your application, you cannot use a media file as a project resource. In your project file, you must instead set the media type to Content and set CopyToOutputDirectory to PreserveNewest or Always.

Images and plugins fail to load when mvc-3 application is published to Windows Azure

When we publish mvc-3 project to windos azure, Is it copy all files and folder under the project name folder, means likes Scripts, Contents, etc ?. I am asking this because i just publish my mvc-3 application to windowsazure and after that when i run it using staging url then it not shown the images, plugin are stop working and when i inspect under chrome then it shows :
If you notice than there are few Scripts ( like sammy.js ) and stylesheets ( like Site.css ) which are present but maximums are not. Can please anybody explain me how can i fix this?
Ok , i found out the root cause, this is happening because each time when i was downloaded the new plugin
i just copy the .js from its source folder and then paste it into my project's Scripts folder. But only after
putting the file inside the Scripts folder does not includes it in the project. You can`t see that file in the
solution explorer until you explicitly include it in the project.There is a button in the solution explorer on the top
of it for show all file, included the files which are not the part of the project.Then Just right click on the
file which you want to included in the project and select option Include in the project. Then only that
file becomes the part of your project. Now you can publish the project and everything will works fine.
Make sure they are included in the .csproj file
Make sure they are marked as Build Action: Content

How to ensure that files are copied from my working directory into bin/Debug rsp. bin/Release?

I have various files in my Visual Studio Solutions that have to be copied to the bin/Debug folder if I change it.
I tried to set Copy to Output Directory - Copy always but it doesnt work. So how can i make sure that when building these files are copied to bin/debug?
Here a screenshot from one example:
Make sure the build action for the files are marked as content, otherwise they will not be copied while building.
Content - The file is not compiled, but is included in the
Content output group. For example,
this setting is the default value for
an .htm or other kind of Web file.
Are you using the Debug profile? The build section of your properties has an output path which is set to bin/debug if you are using this profile.

Categories

Resources