I want to split the components of my applications like:
Application A -> Component X <- Application B
without compiling Component X to a dll.
When I include the .cs, Visual Studio copies it to the application directory. I don't want that. I want to include the file and then use a part of it, like C's #define. For example if I have a ZIP library, I don't want to include the whole assembly if I need a decompression. What's the C# way to make this? Can I somehow tell VS to not copy the file and use #defines or maybe some method attributes?
An assembly is a single, complete file. If you want multiple assemblies which are only included as needed, you need to build them using multiple projects (one per assembly) and reference only the ones you want in the downstream projects you want.
Bigger question, however, is ... Why?
Don't create a problem where there isn't any.
Create a new dll, and MOVE .cs files that should be shared there. Build it, and have AppA and AppB reference and use that dll.
BTW, you can add reference from AppA to AppB, or from AppB to AppA, but not at the same time because it will create circular reference.
And if you want to stick to your idea, LINK your code files as Chris suggested, and use:
#if APPA
// code for AppA
#endif
To have pieces of code compile just in one application. Use project level #defines (project properties) to define APPA and APPB in their respective projects.
You can link to source files in Visual Studio, rather than copying them into in your project. Right click on the folder where you want to put the file, click "Add Existing Item", find the file that you want to add in the dialog, and before hitting Open, note the little down arrow next to the Open button. Click that, and click add as link.
Documentation here: http://msdn.microsoft.com/en-us/library/9f4t9t92(v=VS.100).aspx
Add Existing Item...
Browse to the file you want to include.
Instead of selecting "add", press the arrow to the right of the "Add" button and select "Link" in the drop down.
/B
May be you need Multifile Assembly?
Related
While using Java in Eclipse IDE, one can add a folder to the "Build Path" using the "Add Class Folder" option in the "Libraries" tab, which allows the resources in that folder to get compiled inside the application's jar file, rather than outside or not at all.
.
With this, one can get the resources inside the folder as a URL via the Class.getResource(String name)method. I am already informed about C#'s equivalent: Assembly.GetManifestResourceStream(string name) or Assembly.GetManifestResourceInfo(string resourceName) methods, but I am not aware of C#'s "Build Path" equivalence in Visual Studio (I am using 2019, if you wished to know). Could somebody please explain how I would accomplish Java's build path resource folder in C#?
(Note that I am looking to create a resource folder where anything put inside the folder would be considered an application resource. I am not looking for a way to add one or more resource files individually.)
Any replies would be greatly appreciated! :)
After a little research, I had found a solution for this problem. There are in fact two possible solutions to this issue.
.NET Core Solution
The first involves editing the .csproj file of your C# project. This solution is only available in .Net Core.
You can add this code snippet to your file and change the {PATH_TO_RESOUCE_FOLDER_HERE} folder to your desired folder.
<ItemGroup>
<EmbeddedResource Include="{PATH_TO_RESOUCE_FOLDER_HERE}\**" />
</ItemGroup>
Now any item placed in that folder will be considered an embedded resource Assembly.GetManifestResourceStream(string name) method.
Regular .NET Solution
The second method involves using a .resx file to encapsulate all of your resources
In Visual Studio 2019, you can create a .resx file by right clicking on the location in your project where you wish to add the file to, and navigating to Add > New Item (you may also press Ctrl+Shift+A). You can now navigate to the item that quotes "Resources File" and select it. You can now use this GUI to insert your resources (for a deeper explanation, click on this or this link. For use cases, see this MSDN).
The "Resources File" option
Note that this solution will also work in .NET Core.
I hope this answer helped you as much as it did me! :)
You just create a folder and name it as you like it, say 'Resources'. Add any file you want in there to be treated as a resource by your application.
Then navigate to the properties of every resource file (you can press F4) and in the menu you can choose what you want the compiler do with that file (Compile Action is the option name if I remember well). There you select the type as a resource, the namespace (your Build Path), and whether you like the file to be copied every time you compile your application, and so on.
I create a WindowsRuntimeComponent3 solution, batch build
then VS create a series directories as below, there are WindowsRuntimeComponent3.winmd in these different directories.
just wonder which WindowsRuntimeComponent3.winmd do I need to add?
if them one by one, it will report error
your comment welcome
You add a reference to the project directly, and VS will take care of the CPU architecture and build flavour for you; you shouldn't need to look in those specific directories. When you add the reference, choose "Projects" instead of "Assemblies."
My operating system is Widows 7. I am using Visual Studio 2015.
I have a solution Sln1 which has 2 projects Proj1 and Proj2 both projects are of Class Library type.
In Windows Explorer I can see:
I have a folder c:\MyWork\Sln1 which has the solution file named
Sln1.sln and it also has 2 folders named Proj1 and Proj2.
I have a folder c:\MyWork\Sln1\Proj1 which has the project file named
Proj1.csproj and it also has 3 folders named bin, obj and Properties.
I have a folder c:\MyWork\Sln1\Proj2 which has the project file named
Proj2.csproj and it also has 3 folders named bin, obj and Properties.
I have a 3rd party assembly (Telerik.Web.UI.dll) which is in folder C:\Program Files (x86)\Telerik\UI for ASP.NET AJAX Q3 2015\Bin45.
I want to use this 3rd party assembly in the source code of Proj2 only. But I dont want the project Proj2 to access it from its current location. Instead I want the project Proj2 to have its own copy of this 3rd party assembly. How can I do that?
Copy the dll file to the desired location for example (solution/project/lib). Then in vs2015 open the solution explorer Ctrl + Alt + L.
Then under the project, you want to add the lib to right-click on reference.
Then click on add reference. Go to the browse tab and in the right bottom side click on browse.
Here you can select the dll you want to reference in the project. The 3th party library is now ready to use in your project.
Since you want the 3rd party library in the "Proj2" project location, just copy the DLL file (i.e. Telerik.Web.UI.dll) to a folder in that section (for example under lib, etc.) then in your project(s) go to your Solution Explorer, under the References right click and choose "Add Reference...", then click on Browse and choose your DLL accordingly from the location that you copied earlier.
I hope this will be a useful tip for you.
The most important thing to take into account is version control. No matter where you put it, make sure that the library is under version control, or at least that sufficient information to retrieve it. Also, check that you reference it in your project using relative paths, so anyone can build using only files within his own working copy.
I generally use a structure like this:
\
\libraries
\lib1
\lib2
\source
\proy1
\proy2
So that it's easy to checkout the whole thing, incluiding all dependencies and use those for building everything.
I have this simple line of code in DotSpatial
var raster = Raster.OpenFile("X://Data//4mr_project.tif");
Why raster just getting null value??
I also have .aux, .ovr, .tfw files in the same directory.
EDITED:
I found that the line below works fine:
var featureSet = FeatureSet.Open("X:\\Test Data\\shap\\edited.shp")
because Dotspatial have capability to load .shp file by default. But loading raster data .tif format, Dotspatial need GDAL extensions. Now the question is how to load GDAL extensions manually in Dotspatial using C#.
GDAL extensions can be supported in your own application through the use of the AppManager component. You can drag and drop this onto your form. This allows for support from the GDAL data extensions, and will also gives support to other plug-ins. Here is a basic walk through for adding the AppManager to a new project that just has a Map on the form.
1) From the Visual Studio Toolbox, right click and click on "Choose Items"
2) From the dialog, choose "Browse" and browse to the DotSpatial.Controls.dll library.
3) Click Ok as needed to close the dialogs and get back to the Toolbox.
4) Find the AppManager component you just added in the Toolbox.
5) Drag the AppManager component onto your form. (not on the map, but on the form). A new instance should appear below your form in the non-visual components list.
6) Select this component to view it's properties in the Properties Dialog.
7) Set the map for the appManager (or other components if you are using them).
8) The GDAL component does not even require the Map to be defined in order to work, it should just work. But you will need the GDAL extension. You can find the DotSpatial.Data.Rasters.GdalExtension in the "Windows Extensions" folder. Ensure that you have a similar folder in your output directory with the necessary GdalExtension. One method is just to ensure that this is in your final distribution folder manually.
9) (Optional) One trick you can use, to ensure you have the GDAL plugin in your release folders is to add the libraries as content. This way, regardless if you are working on a debug version or a release version, it will ensure that the GDAL data extension makes it to the output folders.
10) Ensure that the directory you are using (like "Windows Extensions") is listed in the Directories property of the AppManager. The image below shows the default folders which are "Application Extensions" and "Plugins". I think it originally was "Application Extensions" but got updated to "Windows Extensions" later. Unfortunately, I don't think they updated the default folder.
11) In the code somewhere (probably in the form constructor) you need to call appManager1.LoadExtensions(); If you don't call this, it will not actually load the GDAL extension even if you have the GDAL library as part of your project.
12) Add a SpatialDockManager, SpatialHeaderManager, SpatialStatusStrip to the project. Then assign these to the properties on the AppManager, the same way you did the map. For reasons that are beyond me and were implemented after I left, the previously open ended design structure has changed, and now it will throw message box errors if the program does not include these things but you try to use Extensions. The "ProgressHandler" property takes the SpatialStatusStrip.
After following all 12 of these steps (and running the project in x86 mode) the raster code you posted in the initial question works, and you can open geotifs. I also pushed the GDAL extension into the root "Application Extensions" directory while trying to get it to work, but I don't think you have to do that. It should work if it is in a subfolder.
Sorry to be that late (hopefully, it's never too late), but if you wish to use the plugin without using AppManager, because you may be composing something custom and do not want to depend on the main DotSpatial application framework (note that the AppManager utilizes some slightly advanced "magic" to make it all work together), you can do yourself the following few simple tasks:
1) Add a reference to the file
(DotSpatial Release Folder)\Windows
Extensions\DotSpatial.Data.Rasters.GdalExtension.dll
to your project (this is the main GdalExtension Plugin output file).
NOTE: To make sure this step is done correctly, make sure that building your library (the one that references the GdalExtension.dll) ends up copying to this project's output directory the additional files from the same folder (i.e. gdal_csharp.dll etc.).
2) This same folder also contains a gdal subfolder. Copy the folder itself, as-is, to your output path (usually ...\bin\Release\\ or ...\bin\Debug\\, depending on your configuration). Of course in your final project, you would probably like to use a post-build copy event to automate the process, or just include the folder as content in your application build output, as Ted also mentions in step 9 of his answer.
NOTE: By output folder, I am referring to the Application Output Path, not the library output path. If your application is using a library, which undertakes the task of loading rasters (through GdalExtension), the gdal folder does not need to be in the output folder of this library. It needs to end up in your final application's output folder. The reason is that the various dll files are loaded dynamically, so they have to be found in the executing application folder.
3) As early as possible in your codebase, create a new GdalRasterProvider, which should now be referenced by the dll file added in step 1. This means, add something like the following line to your project
var grp = new DotSpatial.Data.Rasters.GdalExtension.GdalRasterProvider();
Thereafter, the first line of code in your post should work as expected. So, technically, the answer to the original question is that the DefaultDataManager class did not find any suitable provider to perform the task of actually loading the Raster file. Therefore, you are left with a null variable.
Interestingly, you don't need to hold the reference anywhere (i.e. do anything with variable grp). If you check the source code, the constructor itself undertakes the task of adding itself to the DefaultDataProvider.PreferredProviders dictionary, which is eventually invoked behind the scenes in the call to Raster.Open(string) method. The only "tough-to-figure-out" part is simply to copy the gdal folder in your application output path, because the GDAL extension loads a number of references located therein upon instantiation of any provider, and the loading is based on a "gdal" subfolder located in whichever folder your application resides and is executed from.
(Note that the Plugin also contains two more providers (GdalImageProvider and OgrDataProvider). To make these two work, you need to instantiate them but also to manually add them to the PreferredProviders dictionary of the DefaultDataProvider, typically also up early in your application code)
I have a solution which contains a website and various class libraries. The exists on the file system like so:
C:\Projects \MyWebsite\dev\MyWebsite.sln
C:\Projects\Core\MyClassLibrary1.csproj
C:\Projects\Core\MyClassLibrary2.csproj
I want to move the App.config file from MyClassLibrary1 project to the bin of the MyClassLibrary2. I want to do this on post build in VS or MSBuild using relative paths if possible. This way anybody checking out the projects will not have to modify any paths if they choose to locate the project in a different location.
I have already tried the following approaches but to no avail.
copy /Y "$(ProjectDir)App.config"
$(ProjectDir)\..\ MyClassLibrary2\$(OutDir)\ MyClassLibrary2.dll.config"
Any ideas?
Thanks
Consider adding the App.config file in your second class library using Visual Studio Add As Link.
Add -> Existing Item -> Add As Link
The Add As Link command is shown if you click the dropdown next to regular Add button in Visual Studio. This way you can reuse the file in multiple projects.
Try some other common MSBuild properties, although João has a better suggestion I think.