Add sound files to build, and find path? C# - c#

In my console based program I am wanting to use a sound file at the end of it, I was able to get that to work however when I publish the project and try send it to others I can't get it to include the sound file, how would I accomplish this?

There are many potential solutions. One of them could be embedding the sound file as a resource.
I recommend writing an installer. Sooner or later you'll need one anyway. Maybe you have seen one of them before. It's that thing that always asks for administrator permissions and you click on the Next button until everything is installed properly.
I'd like to point you to InnoSetup, which is a free, text based installer. That's great to use with version control systems. I especially like it, because I can modify every necessary detail in my build script: just write a line of text to that file with the version number and it simply works.
It's very simple to learn and there are plenty of examples available online. The documentation is great and complete.
What you need is the [Files] section, something like
[Files]
Source: "MYPROG.EXE"; DestDir: "{app}"
Source: "MYSOUND.WAV"; DestDir: "{app}"
And then you can just access the music from the same directory as your executable.
See also the question "List of InnoSetup pages in order with parameters and screenshot" which gives you an impression of the capabilities of InnoSetup.

Add the WAV file to your project (right-click your project in Solution Explorer, select Add | Existing Item...)
In Solution Explorer go to the properties for the file you added,
and for Build Action select "Embedded Resource".
Add the following code to your console app:
Code:
System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream s = a.GetManifestResourceStream("<assemblyname>.<wavfilename>.wav");
SoundPlayer player = new SoundPlayer(s);
player.PlaySync();
replacing <assemblyname> with the name of your assembly, and <wavfilename> with the name of the WAV file that you added.
For my test code, that line looked like this:
System.IO.Stream s = a.GetManifestResourceStream("ConsoleApp3.tada.wav");
because my assembly name is "ConsoleApp3" and the wav file was called "tada.wav".
Note you will also need to add a using System.Media; to the source code file.

Related

Couldn't process file resx due to its being in the Internet or Restricted zone or having the mark of the web on the file

I am facing an issue while debugging c# API Coding in Visual studio 2017. Debugging not started and showing a error message like
Couldn't process file resx due to its being in the Internet or Restricted zone or having the mark of the web on the file. Remove the mark of the web if you want to process these files.
Any idea about this error message?
If you downloaded the file from the internet, either separately or inside a .zip file or similar, it may have been "locked" because it is flagged as coming from the internet zone. Many programs will use this as a sign that the content should not be trusted.
The simplest solution is to right-click the file in Windows Explorer, select Properties, and along the bottom of this dialog, you should have an "Unblock" option. Remember to click OK to accept the change.
If you got the file from an archive, it is usually better to unblock the archive first, if the file is flagged as coming from the internet zone, and you unzip it, that flag might propagate to many of the files you just unarchived. If you unblock first, the unarchived files should be fine.
There's also a Powershell command for this, Unblock-File:
> Unblock-File *
Additionally, there are ways to write code that will remove the lock as well.
From the comments by #Defcon1: You can also combine Unblock-File with Get-ChildItem to create a pipeline that unblocks file recursively. Since Unblock-File has no way to find files recursively by itself, you have to use Get-ChildItem to do that part.
> Get-ChildItem -Path '<YOUR-SOLUTION-PATH>' -Recurse | Unblock-File
Open the file explorer. Navigate to project/solution directory
Search for *.resx. --> You will get list of resx files
Right click the resx file, open the properties and check the option 'Unblock'
Repeat #3 for each resx file.
Reload the project.
None of these answers worked for me, I had to do the following:
Start Menu > type 'Internet Options'.
Select Local intranet zone on the Security tab then click the Sites button
Click Advanced button
Enter file://[computer name]
Make sure 'Require server verification...' is unticked
Source: https://superuser.com/q/44503
Find the path from error log and open the file in explorer
2)select the file and right-click -> properties
Then check the 'unblock' option and click on apply
Complementing #lasse-v-karlsen answer.
To unblock all files recursively, run from powershell as administrator inside the folder you want:
gci -recurse | Unblock-File
source link:
How to Unblock Files Downloaded from Internet? - Winhelponline
https://www.winhelponline.com/blog/bulk-unblock-files-downloaded-internet/
Although this is an older question, I spent several hours tracking down a way to handle this error when it applies to multiple files that are located in sub folders throughout the project.
To fix this for all files within a project, Visual Studio -> Tools -> Options -> Trust Settings and add the project path as a trusted path.
If you are using OneDrive, or any similar network drive, you have 2 options:
1) the easy one is to move the folder to a local directory inside your PC (eg:. C:).
2) but if you want to keep using OneDrive I would recommend to add it to the trusted sites on the internet explorer options and that will fix the problem.
I had this issue on resx files in my solution. I'm using Onedrive. However none of the above solutions fixed it.
The problem was the icon I used was in the MyWindow.resx files for the windows.
I removed that then grabbed the icon from the App Local Resources resource folder.
private ResourceManager rm = App_LocalResources.LocalResources.ResourceManager;
..
InitializeComponent();
this.Icon = (Icon)rm.GetObject("IconName");
This happened after an update to VS2019.
None of the above worked.
The "Unblock" option is not present in the explorer properties.
Recreating file, adding folder (and resx file) to Tools->Options->Trust Settings does not work.
The solution was to copy the project locally (from the network drive).
Solution: Edit and save the file!
From VisualStudio go to the View and expand to see it's resx file
Right-click menu select OpenWith... XML (Text) Editor.
Just add a space at the end and save.
If, like me, you have diligently followed all the above solutions and the error is still there, try closing and reopening Visual Studio.
Obvious, I know, but perhaps I'm not the only one who's become fuzzy-brained after staring at a computer screen all day.
None of the above worked for me.
This happened to me after I added a new button to a toolstrip on a winform. When the button uses the default image of System.Drawing.Bitmap (in image property) this error arose for me. After I changed it to a trusted image (one added to my resource file with 'Unlock' option checked) this error resolved itself.
I use OneDrive as well and I fixed the issue by adding Microsoft/Onedrive domains to trusted sites.
Internet options->Security->Trusted Sites->Sites-> and add there:
https://marq-my.sharepoint.com
https://*.onedrive.com
https://*.office365.com
https://*.office.com
After that: Close->Ok->Restart PC
References:
https://www.marquette.edu/its/help/onedrive-for-business/onedrive-for-business-as-a-trusted-site.shtml
https://community.spiceworks.com/topic/2230091-add-onedrive-directory-as-trusted-documents-location
I stumbled upon another possible reason of this error. If you use NTFS symbolic links in your project tree, and probably subst'ed drives, you may get this error even if they point to your local drive. If this is the case, try to avoid the situation when .resx files are reached via symlinks.
None of the suggestions above worked for me so I created a new file with a slightly different name and copied the contents of the offending file into the new file, renamed the offending file and renamed the new file with the offending file's name. Worked like a charm. Problem solved.
If the above solutions fail, like they did for me, backup all resx files and then delete them. Then try running the project again.
Its works perfect by using Powershell command as below (It's very powerful cammand to Unblock all files inside a project folder). Below is the example how to use it:
Powershell cammand:
PS C:\Users\kri> Get-ChildItem -Path 'C:\Users\kri\Downloads\ProjectFolder' -Recurse | Unblock-File
The only solution that worked for me was to copy the solution to a folder outside OneDrive, build and run the solution. Finally, copy the folder back to OneDrive
I also faced this issue when i tried to run a downloaded zipped code from github.
Solution: I simply cloned the code from github instead of downloading and code run successfully without errors.

Edit App-V archive file

I'm trying to replace a file in a .appv archive file.
I know you can just rename the file to .zip extract and replace the file.
But when I zip it again and rename it back to .appv the file size is different and i get the following error when opening.
Im trying to change the AppxManifest.xml file or a setting within that without having to open via the UI(cmd, c# or powershell).
Thanks
Open up your sequencer and choose "Modify"
"Update"
Then I am skipping the steps where you upload your .appv file and installer (if you don't need the files from installer, you can always ignore it)
"Continue to modify"
Close the last page and it will automatically jumps to this page
Now, you can open up your package, expand the folder and add files if you want
The best option is to use one of the application packaging tools that supports editing of App-V package. Apart from Advanced Installer that has been already mentioned, take a look at AdminStudio and PACE Suite - both support editing App-V. There may be even more than those two I know.
You cannot zip a package back and expect to work, this is not supported by Microsoft.
Starting with App-V 5.1 the Sequencer from Microsoft can export and import the manifest file from the package, so you can modify it, but from what I know you cannot script this, it must be done from the GUI. The list of powershell cmdlets for the sequencer is quite short.
Also, Advanced Installer can also create and edit App-V packages much easily and faster than the sequencer, but again only from the GUI.
You can also try a free tool, called TweakApp-V, here's an example of you can use it. It has predefined commands to add/delete files and registry.

How to add source file (.cs) to resources (.resx) in a pre-build event?

Is it simple or even possible?
Why: Following my other question and using this approach, now I would like to simply embed the source file to the resources so I can copy it to the temp folder while the application is running - and keep the application as a single file.
I assume using a pre-build event is the best way to do this. If so, how would it be done?
EDIT: I'm asking about adding it before building because I also assume it won't be automatically updated if I add it manually once and then change the code after.
I recently went through this same issue when developing an examples suite for a .NET control which had to display its own example source code in the application.
The approach I ended up with was as follows.
I created a batch script (run in pre-build) to copy all the code files under /Examples/* to another folder in the solution Resources/ExamplesSrc/*
I then included all these files under Resources/ExamplesSrc/* in the csproj and saved it.
The next step was I set all the files under Resources/ExamplesSrc/* as embedded resource and committed the change to SVN. I did not commit the examples (generated) to SVN, just the modified csproj and batch file.
The effect when developers checked out was that they had (!) icons on all the resources (they were not on disk, but the csproj was looking for them), however as soon as they built the generated files appeared and all was well. This approach also worked with a build server (Team city) and the examples suite can now load and browse its own code via embedded resources.
One caveat you should be aware of. There is a quirk (by design apparently) in VS2010. If an embedded resource has ".cs" in it (i.e. any code file!) it cannot be loaded from the assembly using Assembly.GetManifestResourceStream(). To work around this all source files were renamed in the batch copy step from *.Xaml.cs to *.Xaml.c.txt, from *.cs to *.c.txt
Hope this helps!
Turns out I don't need to (re)add the source file each time I build the solution!
When I add it manually once it becomes a "Text" file on resources - and I can easily access it as a string using Properties.Resources.SourceCode (having the file named "SourceCode.cs"). And yes, it is updated automatically since the resource property "Persistence": the file is linked at compile time.

Possible to remove and add a reference to csproj programmatically via a batch file?

I am writing a short batch file to prepare a control library DLL with Examples project for deployment via sip file and have the following question.
Given a csproj file at known location and a DLL at known location, is it possible to programmatically update the csproj from the batch file (via third party command line exe, or other scripting) to add the new DLL?
My folder structure is
/Build
/SDK
/WPF
/4.0 : ControlLibrary.dll sits here
/Examples
/WPF
/4.0 : Examples.csproj sits here
Assuming the batch file is at the /Build level, is there any way to modify Examples.csproj to reference ControlLibrary.dll ?
Just to clarify, the reason why I have the structure like this is I wish to deploy an examples csproj to ship with my control library. Examples.csproj should reference the obfuscated control library under SDK/. Examples.csproj also exists on dev trunk (where it has been copied from) and in the development solution it references output of ControlLibrary.csproj on non obfuscated form.
Essentially what im creating here is a folder structure to zip up and ship the ControlLibrary plus examples, hence, the need to update the reference.
Update - Solved using Powershell
Please see this related question and answer on adding and removing references using Powershell
csproj files are XML files - you can easily write a command line application to manipulate these files (adding, removing nodes etc).
You can call such a command line application from your batch file.
You can find the schema for this file at:
%windir%\Microsoft.NET\Framework\[framework version]\Microsoft.Build.xsd
As described in this SO answer.
I don't understand why you would need to modify the csproj file in your case. Just make sure the library reference in the csproj file is relative, i.e. ..\..\..\SDK\WPF\4.0\ControlLibrary.dll and it will keep working fine even if you move the complete folder hierarchy to a new location.
If you're trying to simplify adding the library to new projects though, take a look at NuGet. It's the best way for distributing and deploying libraries.

Why does VS2010 allow for the concept of "include in project"?

I'm still learning the basics of how VS2010 sees the world. Apparently, you can optionally "include" a file in a project. I'm a bit confused by this: If a file is version-controlled, AND the file is within the project directory, shouldn't it implicitly be "included" in the project? If not, what's the use case where a version-controlled file in the project directory should NOT be included in the project?
=== Addition ===
Based on the answers I've gotten so far, maybe I should rephrased my question: What does it mean for a file to be "included" in a project?
A project needs to know about files in order for compilation and distribution to occur. Just because you have a file that's under source-control, doesn't mean that it will be compiled if the project is unaware of it.
Also, you may want to include files as part of a distribution package. We do this quite often for our web projects that we distribute using web app gallery.
Conversely, you could have documentation or sql scripts that you version control, but do not want them to be part of the project.
EDIT: In answer to your update, what it means for a file to be included in a project is that the file is actually added to the .csproj or .vbproj file and will be used during compilation and/or distribution. VS does differentiate if the file is Content or if it needs to Compile it. This can be seen by clicking on the file in Solution Explorer and looking at the Build Action property.
No, you don't want random files that happen to be in the project directory included in source control.
We do sometimes put documentation (pdfs) or drawings/schematics in the project folder and under version control but you don't need them inside the visual studio project (especially when they are not being distributed because they are for internal use only).
Excluding the file from your project can be useful if the file is related to the project but not necessarily needed in the solution.
Example
If I need some test XML for an application that i'm writing; that is designed to normally be pulling this from a WCF service, it can be useful to keep that file in the directory for a development environment where I use IO to get the XML for testing, but I don't necessarily want it in my solution which is source controlled.
When you exclude a file from a project is no longer compiled or embedded, then when you want to include it again you can do so without having lost your settings.
If you e.g. copy a file (containing a helpful class which want to have in your project) into a folder of your project, then you will see ... nothing. You have to check the option "Show all files" of the solution explorer and the copied file can be seen, but it is still "greyed out". No you can choose the menuitem Include in project and that file will be integrated in your project and a pending change (add) for your source control is added too. Visual Studio doesn't include all files it can find in the project folder automatically to the project - and that is a good feature.
One of my colleagues explained to me a scenario in which a version-controlled file should NOT be part of the project. Here's the idea:
A developer writes some new code.
The code is experimental, and not intended to be part of the normal build.
The easiest way to exclude the file from the build is to NOT include it in the project, but still version-control it.
This way, the file can be shared with other developers, but not break the build.

Categories

Resources