Library reference missing for WindowsInstaller C# Console Application - c#

New to C# and VS
All I am trying is to get details/properties of an msi file programmatically (eg. "Comments" in the details tab when you get file properties)
I am using the following code (using someone's code of internet) , VS is complaining about missing reference for below and also suggesting not to use "using"
using WindowsInstaller;//msi.dll
ERROR
The type or namespace name 'type/namespace' could not be found (are you missing a using directive or an assembly reference?)
And also within the function it cannot resolve any reference for the following
WindowsInstaller.Installer installer = (WindowsInstaller.Installer)Activator.CreateInstance(installerType);
Here is the code:
using System.Configuration.Install;//System.Configuration.Install.dll
using WindowsInstaller;//msi.dll
static void get_msi_details(string path)
{
// Get the type of the Windows Installer object
Type installerType = Type.GetTypeFromProgID("WindowsInstaller.Installer");
// Create the Windows Installer object
WindowsInstaller.Installer installer = (WindowsInstaller.Installer)Activator.CreateInstance(installerType);
// Open the MSI database in the input file
Database database = installer.OpenDatabase(path, MsiOpenDatabaseMode.msiOpenDatabaseModeReadOnly);
// Open a view on the Property table for the version property
View view = database.OpenView("SELECT * FROM Property WHERE Property = 'Comments'");
// Execute the view query
view.Execute(null);
// Get the record from the view
Record record = view.Fetch();
// Get the version from the data
string version = record.get_StringData(2);
}
VS studio isnt offering anything in "Potential Fixes" to resolve the reference.

In Visual Studio go to References > Add Reference... > COM > Browse... > %WINDIR%\system32\msi.dll.

Related

UWP: The type or namespace name 'TensorFloat' could not be found

I am trying to run a UWP app I made (which worked) on a different computer, and I am now getting the error:
The type or namespace name 'TensorFloat' could not be found (are you
missing a using directive or an assembly reference?)
I have VS 2017, my Windows 10 version is 1809, build 17763.194, and I've installed Windows 10 SDK 10.0.17763.132, but it didn't help. Is there a package I need to install or something like that?
Edit:
Changing the target version fixed it, now those errors are gone and the project starts to build. However, it now breaks at
public static async Task<modelModel> CreateFromStreamAsync(IRandomAccessStreamReference stream)
{
modelModel learningModel = new modelModel();
learningModel.model = await LearningModel.LoadFromStreamAsync(stream);
learningModel.session = new LearningModelSession(learningModel.model); // it breaks here
learningModel.binding = new LearningModelBinding(learningModel.session);
return learningModel;
}
and I get a very strange error:
System.Runtime.InteropServices.COMException: 'Unspecified error
No suitable kernel definition found for op Sub (node Minus675)'

System.Printing not found( C# )?

I am trying to run the following example from MSDN:
using System.Printing;
public class PrintTest {
public static void Main(string[] args)
{
// Create the printer server and print queue objects
LocalPrintServer localPrintServer = new LocalPrintServer();
PrintQueue defaultPrintQueue = LocalPrintServer.GetDefaultPrintQueue();
// Call AddJob
PrintSystemJobInfo myPrintJob = defaultPrintQueue.AddJob();
// Write a Byte buffer to the JobStream and close the stream
Stream myStream = myPrintJob.JobStream;
Byte[] myByteBuffer = UnicodeEncoding.Unicode.GetBytes("This is a test string for the print job stream.");
myStream.Write(myByteBuffer, 0, myByteBuffer.Length);
myStream.Close();
}
}
but the compiler is complaining
The type or namespace Printing does not exist in the namespace
System(are you missing an assembly reference) ?
How do I solve this issue ?
EDIT: How do I add a reference for command line compiled application ( Not Visual Studio)
From the command line something like csc /reference:lib\System.Printing.dll
Original Answer
Project > Add Reference, then under 'Assemblies > Framework'.
Choose System.Printing.
You can find out which Assembly you need to add a reference to by Googling the namespace followed by the word 'assembly'. In your case:
System.Printing assembly
The second result is from MSDN and indicates which assembly System.Printing can be found in.
From Command prompt
Create a public reference
/reference:[alias=]filename
/reference:filename
Where
Arguments
filename
The name of a file that contains an assembly manifest. To import more than one file, include a separate /reference option for each file.
alias
A valid C# identifier that will represent a root namespace that will contain all namespaces in the assembly.
Microsoft documentation page for cmd
From Visual Studio Community 2013 (Free version)
Right click References folder in your solution and browse for it there.
You are missing an assembly reference. Add a reference to System.Printing.dll.
You need to add the System.Printing assembly to your project.
You can find this by right clicking on your project and clicking "Add Reference". (You can search for it in the Assemblies > Framework tab)
Additionally, you must add using System.IO; in order to use Stream.
In your Solution Explorer right click on References click on Add Reference click on the .NET tab and scroll to System.Drawing. It should work.
Original answer found here:
https://social.msdn.microsoft.com/Forums/vstudio/en-US/0648fdc4-f67b-476e-b434-998efec14b89/class-systemprintingprintcapabilities-not-found?forum=wpf
However, you'll need to add the Assembly called ReachFramework

How to add typed Dataset to visual studio project programmatically and keep design support

After having created a Typed DataSet for a given set of database tables (using System.Data.Design.TypedDataSetGenerator).
I can attach the generated C# file with
// VsProj is of type VsLangProj.VSProject for the current project
// artifactPath is the path to the newly created C# artifact with the
// typeddataset generated
// code.
VsProj.Project.ProjectItems.AddFromFile(artifactPath);
However when trying to open this in the designer it fails (it is certainly missing the .xsd, .xsc, .xss files).
Is there a documented way to get the .xsc / .xss files?
Ok, I found it, instead of using System.Data.Design.TypedDataSetGenerator, just set some magic properties for the ProjectItem and voila:
// xsdPath is a path to a .xsd file which was generated with System.Data.DataSet.WriteXmlSchema.
ProjectItem pi = _vsProj.Project.ProjectItems.AddFromFile(xsdPath);
// this little magic replaces having to use System.Data.Design.TypedDataSetGenerator
pi.Properties.Item("SubType").Value = "Designer";
pi.Properties.Item("CustomTool").Value = "MSDataSetGenerator";
(Of course if you were outside of Visual Studio, then it's better the previous method).

I can't read my embedded XML file from my second C# project

I have a Visual Studio Solution with two projects inside.
The first project is a windows service, and this project contains one XML file inside a folder (called Configurations). This XML file is called Databases.xml. I have changed Databases.xml Build Action from content to embedded resource, and now I want to access this XML file from my other project in my solution, which is a WPF application.
I have therefore added an reference to my windows service project inside my WPF project, and would now like to access my XML file from my WPF project.
My problem is that when I am trying to access the embedded resource then I can't find out which type to use and what the path/namespace to my assembly and XML file should be. When I am using the
string[] names = this.GetType().Assembly.GetManifestResourceNames();
my names array is filled out with some resources from my WPF project. What I want is to access the ResourceNames and of course my Databases.xml file from my Windows Service project.
Please help me since this problem is driving me nuts.
If you need any additional information, please let me know.
My Solution Update 26-07-2013
I found out that the real problem occured when I couldn't use my first windows Service projects namespace as a type for my assembly. my Windows Service consists of a service class (with OnStart() and OnStop() method inside), and in order to use this class as my namespace type, I needed to add another reference to my WPF project. I needed to add a reference to System.ServiceProcess namespace, in order to use my Windows Service Class as a type for my assembly in my WPF Project.
In Order to access my Databases.xml file, I have come up with this solution. Remember to insert your own projects name and class name instead of my placeholders (<Windows Service Project Name> etc).
//Remember to add a reference to System.ServiceProcess in order to be able to use your WIndows Service Project as an assembly type.
using (Stream stream = typeof(<Windows Service Project Name>.<Windows Service Class Name>).Assembly.GetManifestResourceStream("<Windows Service Project Name>.<Folder Name>.Databases.xml"))
{
//Load XML File here, for instance with XmlDocument Class
XmlDocument doc = new XmlDocument();
doc.Load(stream);
}
So my real problem was that I didn't include the System.ServiceProcess reference to my second project.
You've to refer to Windows Service project Assembly to make it work
The problem with your code is This.GetType().Assesmbly gives current Assembly In your case WPF Assembly and obviously you'll not find what you need there.
Try this
Assembly windowsServiceAssembly = typeof(SomeTypeFromThatAssembly).Assembly;
string[] names = windowsServiceAssembly.GetManifestResourceNames();
Hope this helps.
If your class is static class then use this methods:
internal static string GetFromResources(string resourceName)
{
var asm = Assembly.GetExecutingAssembly();
var resource = asm.GetManifestResourceNames().First(res => res.EndsWith(resourceName, StringComparison.OrdinalIgnoreCase));
using (var stream = asm.GetManifestResourceStream(resource))
{
if (stream == null) return string.Empty;
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
For example if your embedded resource file names on the this project is 'MyFile.txt' then use this static method same this code:
var myFileData = GetFromResources("MyFile.txt");

How to execute SQLCLR UDF added directly to SSDT sqlproj and resolve reference error SQL71501

I have been trying to find the answer to this simple question - but so far couldn't figure it out.
Let say I have MyDb.sqlproj, with various sql content (sprocs, views, trigger, etc).
I have added a new UDF through Add-> New item -> SQL CLR C#, User Defined Function.
For example:
namespace MyNameSpace
{
public class MyClass
{
[SqlFunction(DataAccess = DataAccessKind.Read)]
//I use different attributes here, but it doesn't matter
public static int Method1()
{
return 0;
}
}
}
In MyDb.sqlproj Properties, SQLCLR tab MyDb is the name of assembly and default namespace
In my sql code I call the clr method using EXTERNAL NAME:
CREATE PROCEDURE ClrMethod1
RETURNS [int] WITH EXECUTE AS CALLER
AS EXTERNAL NAME [MyDb].[MyNamespace.MyClass].[Method1]
I seem to tried everything to get the last line to compile. It cannot resolve the reference and get:
SQL71501: Function: [name of my sql function] has an unresolved
reference to Assembly [MyDb]
Please point me to the right way to get it working. What could I be missing?
I am using VS2010 SP1 and latest version of SSDT
You must add your compiled DLL containing your CLR code as a Reference. So under your MyDb SSDT project->References(right click)->Add Reference, browse to the DLL.
You could probably get away with referencing the project instead of the DLL if you had the CLR(class library) project in the same solution, but in my case I am referencing a DLL(compiled for a seperate solution).
As far as the format of the AS EXTERNAL NAME line:
AS EXTERNAL NAME [AssemblyName].[ClassName].[FunctionName]
Note: For CLR objects I remove the namespaces from around my classes/code just to simplify things, because this step is usually the most problematic. It is easy to confuse the AssmeblyName/DLL Name/Namespace. The AssemblyName is found in your CLR class library project by accessing the Project Properties->Application->"Assembly Name:". I would remove any non alpha-numeric/spaces from it just to simplify the name and rule out that as a problem.
So I would try that, and once you get that working, if you really want namespaces, then you can add the namespace and figure out the syntax from there, and at least you know the other parts are correct.
Ok reallized that you have a *.cs file actually inside the same SSDT project. so in that case if your code is this:
CS file:
public partial class UserDefinedFunctions
{
[Microsoft.SqlServer.Server.SqlFunction]
public static SqlString SqlFunction1()
{
// Put your code here
return new SqlString (string.Empty);
}
}
SQL file:
CREATE PROCEDURE ClrMethod1
RETURNS [int] WITH EXECUTE AS CALLER
AS EXTERNAL NAME [MyDB].[UserDefinedFunctions].[SqlFunction1]
This compiles for me. Note: Again no namespace was used. When I did Add New Item... the generated code did not come with a namespace.

Categories

Resources