using System.Net.NetworkInformation errors - networking

I want to use NetworkInformation namespace and when I use system.net.networkInformation I'm getting error: "The type or namespace name 'NetworkInformation' does not exist in the namespace 'System.Net' (are you missing an assembly reference?) "
I'm compact framework v2.0 or 3.5. compact framework should support networkInformation namespace?
I also tried to use this code:
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "ipconfig.exe";
p.Start();
p.WaitForExit();
string output = p.StandardOutput.ReadToEnd();
return output;
but the StandartOutput and redirectStandartoutput doesn't exist.
I'm trying to find out is the LAN is up or down. There is other way that I can use with compact framework?

You can do this with OpenNetCF's Smart Device Framework. I've used classes in this framework on several occasions when I find they are missing from CF2.0
See #ctacke's answer here:
The easiest way is to use OpenNETCF's SDF and look at the
OpenNETCF.Net.AdapterStatusMonitor class, which will raise events when
NDIS sends out notifications (like MEDIA_CONNECT and
MEDIA_DISCONNECT).

Related

Xamarin.Forms Portable + Mono.Data.Sqlite.Portable

Good day to everyone,
I'm building an application in Xamarin.Forms Portable for iOS,Android,Windows Universal(10) .
I'm trying to use the Mono.Data.Sqlite.Portable, but I'm not able to create or connect to a local database, I've tried many solutions to fix the problem, but until now I dint find the solution..
The error is trow at "connection.open();"
The Code is :
var config = DependencyService.Get<DB.IConfig>();
var cnn = config.DirectoryDB;
using (var connection = new SqliteConnection("" +
new SqliteConnectionStringBuilder
{
DataSource = "URI=file::memory:",
Version=3,
}))
{
//connection.ChangeDatabase("hello.db");
connection.Open();
using (var transaction = connection.BeginTransaction())
{
var insertCommand = connection.CreateCommand();
insertCommand.Transaction = transaction;
insertCommand.CommandText = "CREATE TABLE TESTE(teste as varchar(10))";
insertCommand.ExecuteNonQuery();
transaction.Commit();
}
}
on the "DataSource" I've a local path, and other paths.
(Ex:. C:\Users\MyMachineName\AppData\Local\Packages\f736c883-f105-4d30-a719-4bf328872f5e_nh7s0b45jarrj\LocalState\hello.db)
The error is :
An exception of type 'System.NotImplementedException' occurred in Mono.Data.Sqlite.dll but was not handled in user code
Additional information: The method or operation is not implemented.
ImageError
Connection Extra Information
The last picture, it may give more information....
Thank you all in advance :)
I've found the solution,I hope it helps the people that may be looking for the same as I.
Just go to Nuget - And download the Portable.Data.Sqlite that relies on SQLitePCL (Latest version) .
This package (Portable.Data.Sqlite) it will use the connection and will create SqliteDataReader and other property's that we are used to use .
This will work on Visual Studio 2015 and the project must be in framework 4.51 +=
----Error Tips----
if you encounter this error : Cant use Temporary Database or Path, just download Sqlite.dll and place on the project BIN folder and System32/ SysWoW64 .

EF4: AutoDetectChangesEnabled not found

I'm in weired situation. I read about context.Configuration.AutoDetectChangesEnabled = false; and decide to use it.
But I cant found it. the code is
using (DefaultCS db = new DefaultCS())
{
db.Configuration.AutoDetectChangesEnabled = false;
order.OrderTables = TableNo;
order.OrderMenus = oMenu;
db.Orders.AddObject(order);
db.SaveChanges();
}
I got error at db.Configuration which is ROS.DefaultCS does not contain a definition for 'Configuration' and no extension method 'Configuration' accepting a first argument of type 'ROS.DefaultCS' could be found (are you missing a using directive or an assembly reference?)
what I'm missing?
I'm using EF4.
Here is the answer: in Entity Framework 4 we should use db.Orders.MergeOption = MergeOption.NoTracking; instead of db.Configuration.AutoDetectChangesEnabled = false; which is only applicable in Entity Framework 5.
Code for EF4 and EF5
In EF4
db.Orders.MergeOption = MergeOption.NoTracking;
In EF5
db.Configuration.AutoDetectChangesEnabled = false;
The downgrade for EF4 is to set it for each Entity.

How to get the keyword from category name in C# TBB?

I am trying to fetch the keyword present in the Category using C# TBB to use the output in following DWT TBB.
For that I have a component with the Category field.
I am trying to write the following C# TBB to get the keyword value.
<%#Import NameSpace="Tridion.ContentManager.Templating.Expression" %>
try
{
string className = package.GetValue("Component.Fields.title");
KeywordField keywordField = package.GetKeywordByTitle(className);
package.PushItem("Class", package.CreateStringItem(ContentType.Text, keywordField.Value.Key));
}
catch(TemplatingException ex)
{
log.Debug("Exception is " + ex.Message);
}
But I am getting following compilation error.
Could not compile template because of: error CS0246: The type or namespace name 'KeywordField' could not be found (are you missing a using directive or an assembly reference?) error CS1061: 'Tridion.ContentManager.Templating.Package' does not contain a definition for 'GetKeywordByTitle' and no extension method 'GetKeywordByTitle' accepting a first argument of type 'Tridion.ContentManager.Templating.Package' could be found (are you missing a using directive or an assembly reference?)
Please suggest me how can I achieve it?
Thanks in advance
Af Jeremy suggested you should study API, I am providing you example of getting keywords from categories. Hope it may help
Include files
using Tridion.ContentManager;
using Tridion.ContentManager.CommunicationManagement;
using Tridion.ContentManager.Templating.Assembly;
using Tridion.ContentManager.ContentManagement;
using Tridion.ContentManager.ContentManagement.Fields;
using Tridion.ContentManager.Templating;
Sample Code, You can use Key and Value from loop here as per your requirement.
string catID = package.GetByName("CategoryID").GetAsString();
TcmUri catURI = new TcmUri(int.Parse(catID), ItemType.Category, PubId);
var theCategory = m_Engine.GetObject(catURI) as Category;
catKeywords = GetCatKeywords(theCategory);
string strSelect = "<select>";
foreach (Keyword k in catKeywords)
{
k.Key // Keyowrd key
k.Value // KEyword Value
}
//keyword list
private IList<Keyword> GetCatKeywords(Category category)
{
IList<Keyword> keywords;
if (!Utilities.IsNull(category))
{
Filter filter = new Filter();
filter.BaseColumns = ListBaseColumns.IdAndTitle;
keywords = category.GetKeywords(filter);
if (!Utilities.IsNull(keywords))
{
return keywords;
}
}
return null;
}
The error message is absolutely clear what the problem is - there's no reference to the KeywordField class. You need to import the relevant namespace:
<%#Import NameSpace="Tridion.ContentManager.ContentManagement.Fields" %>
Also absolutely clear is the fact that the Package object doesn't have a method called GetKeywordByTitle. There is a GetByName method, but this is for retrieving a named item from the Package, not for getting an object from the respository.
Tridion.ContentManager.ContentManagement.Category does have a GetKeywordByTitle method, but to use this you will have to get the category first, which will likely mean having to know the URI of the category.
Perhaps you need to study the API docs some more?
"GetKeywordByTitle" is not a method on Package, its a method on Category.
Can't you just new-up Keyword?
string selectedKeyword= package.GetValue("Component.Fields.title");
Keyword keyword = new Keyword(selectedKeyword, engine.GetSession());
Cheers

Using System.Reflection

Im loading an Exe into my application using Assembly.LoadFile().
From that is it possible to get the Method of a particular class from that EXE.
Thanks in advance .
Try:
var assembly = Assembly.LoadFile("C:\path-to-some-app.exe");
var desiredType = assembly.GetType("SomeNamespace.SomeClass");
var methodInfo = desiredType.GetMethod("MethodName");
A number of things you can do with Reflection (Including constructors, method invocation etc.) Tutorial at : http://www.csharp-examples.net/reflection-examples/

Is there a way to get ALL the MIME types instead of wrinting a huge case statement?

I want to populate
Response.ContentType = "text/plain";
From somewhere in the server/web/dictionary ALL possible MIME types according to file extension:
public string GetMimeType(string extension)
{
//This is what I am looking for.
}
Also, I have to rename the file (at least if going to be downloaded, so I have to know in advance if it's going to be opened or not.
You can store the mimetype when the file is uploaded ( FileUpload.PostedFile.ContentType ) and send that when the file is requested.
Umm... why? You're not going to be returning content of every possible type, are you?
Here's a list of common types: http://www.webmaster-toolkit.com/mime-types.shtml. There is no list that would include "ALL" types simply because any application vendor can create a custom one and associate it with a custom extension.
It's going to depend on your platform. Here's one for C# and IIS: http://blog.crowe.co.nz/archive/2006/06/02/647.aspx
In Powershell it's a one-liner:
([adsi]"IIS://localhost/MimeMap").MimeMap
The code in the link posted by Richard:
// Maintain a sorted list to contain the MIME Types
SortedList sl = new SortedList();
Console.WriteLine("IIS Mime Map - c#");
Console.WriteLine();
// Serve to connect to...
string ServerName = "LocalHost";
// Define the path to the metabase
string MetabasePath = "IIS://" + ServerName + "/MimeMap";
// Note: This could also be something like
// string MetabasePath = "IIS://" + ServerName + "/w3svc/1/root";
try
{
// Talk to the IIS Metabase to read the MimeMap Metabase key
DirectoryEntry MimeMap = new DirectoryEntry(MetabasePath);
// Get the Mime Types as a collection
PropertyValueCollection pvc = MimeMap.Properties["MimeMap"];
// Add each Mime Type so we can display it sorted later
foreach (object Value in pvc)
{
// Convert to an IISOle.MimeMap - Requires a connection to IISOle
// IISOle can be added to the references section in VS.NET by selecting
// Add Reference, selecting the COM Tab, and then finding the
// Active DS Namespace provider
IISOle.MimeMap mimetypeObj = (IISOle.MimeMap)Value;
// Add the mime extension and type to our sorted list.
sl.Add(mimetypeObj.Extension, mimetypeObj.MimeType);
}
// Render the sorted MIME entries
if (sl.Count == 0)
Console.WriteLine("No MimeMap entries are defined at {0}!", MetabasePath);
else
foreach (string Key in sl.Keys)
Console.WriteLine("{0} : {1}", Key.PadRight(20), sl[Key]);
}
catch (Exception ex)
{
if ("HRESULT 0x80005006" == ex.Message)
Console.WriteLine(" Property MimeMap does not exist at {0}", MetabasePath);
else
Console.WriteLine("An exception has occurred: \n{0}", ex.Message);
}
// Convert to an IISOle.MimeMap - Requires a connection to IISOle
// IISOle can be added to the references section in VS.NET by selecting
// Add Reference, selecting the COM Tab, and then finding the
// Active DS Namespace provider
According to my googling: (lost the links, sorry)
The "Active DS IIS Namespace Provider" is part of the IIS installation.
After you install IIS you will see that in the list of options.
If you don't see it should be located at C:\windows\system32\inetsrv\adsiss.dll.
To install IIS:
click Start, Settings, Control Panel, Add or Remove Programs, Add or Remove Windows Components, select Internet Informatoin Services (IIS).
Most of the code I've seen uses some combination of these:
using System.IO;
using System.DirectoryServices; // Right-click on References, and add it from .NET
using System.Reflection;
using System.Runtime.InteropServices;
using System.Collections;
using IISOle;
using System.Collections.Specialized;
The Active DS Namespace might be under the COM tab when adding the reference.
I've written a small class based on the webmaster-toolkit.com list. This is to avoid using COM and the IIS route or any IIS references.
It uses an XML serialized list which contains about 400 mimetypes, so is usually more than enough unless you have really obscure mimetypes. In that case you can just add to the XML file.
The full solution can be found here. Here's a sample:
class Program
{
static void Main(string[] args)
{
var list = MimeType.Load();
MimeType mimetype = list.FirstOrDefault(m => m.Extension == "jpg");
}
}

Resources