I'm currently using C# to set the custom attributes of multiple excel files. I'm using an imported library from Microsoft known as DSOFile to write to the CustomProperties property. One issue that I'm running into is whenever the code attempts to write to an excel file that already has custom properties written to it, such as the Company and Year, a COMException exception is thrown to indicate the custom properties of the file already has a field with that name. Exact Message: "An item by that name already exists in the collection". I would like to be able to delete that item in the collection so that I can rewrite to the file. For example, if I accidentally added the wrong year to the year attribute in the file, I would like the ability to clear that field and write a new value to it. I was unable to find a method in the DSOFile class that removes metadata. Is there anyway to "programmatically" clear metadata from a file without doing it through the file properties window?
Sample Code:
DSOFILE.OleDocumentProperties dso = new DSOFile.OleDocumentProperties();
dso.Open(#"c\temp\test.xls", false, DSOFile.dsoFileOpenOptions.dsoOptionDefault);
//add metadata
dso.CustomProperties.Add("Company", "Sony");
dso.Save();
dso.Close(false);
If you want to change the default properties used by Office like Company or Author, you can just update them via the SummaryProperties object:
OleDocumentProperties dso = new DSOFile.OleDocumentProperties();
dso.Open(#"c:\temp\test.xls", false, DSOFile.dsoFileOpenOptions.dsoOptionDefault);
//Update Company
dso.SummaryProperties.Company = "Hello World!";
dso.Save();
dso.Close(false);
Note, that you cannot change the default properties of documents that you can access via the SummaryProperties object via the CustomProperties object in dso. The CustomProperties are meant for additional properties used by the user, not the ones already introduced by Microsoft Office.
In order to change the custom properties, you have to be aware that CustomProperties is a collection that you can iterate over via foreach. So you can use the following two methods:
private static void DeleteProperty(CustomProperties properties, string propertyName)
{
foreach(CustomProperty property in properties)
{
if (string.Equals(property.Name, propertyName, StringComparison.InvariantCultureIgnoreCase))
{
property.Remove();
break;
}
}
}
private static void UpdateProperty(CustomProperties properties, string propertyName, string newValue)
{
bool propertyFound = false;
foreach (CustomProperty property in properties)
{
if (string.Equals(property.Name, propertyName, StringComparison.InvariantCultureIgnoreCase))
{
// Property found, change it
property.set_Value(newValue);
propertyFound = true;
break;
}
}
if(!propertyFound)
{
// The property with the given name was not found, so we have to add it
properties.Add(propertyName, newValue);
}
}
Here is an example on how to use UpdateProperty:
static void Main(string[] args)
{
OleDocumentProperties dso = new DSOFile.OleDocumentProperties();
dso.Open(#"c:\temp\test.xls", false, DSOFile.dsoFileOpenOptions.dsoOptionDefault);
UpdateProperty(dso.CustomProperties, "Year", "2017");
dso.Save();
dso.Close(false);
}
Related
I save local user settings to xml file. Program contains "Settings" class that serialize when the program is closed and deserialize when it is started next time.
But the problem is that the program is changed all the time, and when I create next version - I want the user settings to be saved. But the program may contains new fields of settings, and then the program will started and deserialised the old xml file - new fields will be null.
Now I check every fields as hard-code in the program, as like:
Settings sts = (Settings)Deserialise(path);
if(sts.Field2 == null) sts.Field2 = "defaultvalue2";
if(sts.Field3 == null) sts.Field3 = "defaultvalue3";
Of course it is not satisfied for me. Is it possible to do "default" value of a variable as the same time when I change code of Settings class? Like this:
class Settings
{
public string Field1 (DefaultValue: "defaultvalue1");
public string Field2 (DefaultValue: "defaultvalue2");
}
public void Main
{
Settings sts = (Settings)Deserialise(path);
foreach(var fld in typeof(sts))
{
if(fld.Value == null)
fld.Value = Settings.Fields[fld].DefaulValue;
}
}
Yes it is possible, simply use the standard way to set standard values:
class Settings
{
public string Field1 = "defaultvalue1";
public string Field2 = "defaultvalue2";
}
public void Main
{
Settings sts = (Settings)Deserialise(path);
/* not needed
foreach(var fld in typeof(sts))
{
if(fld.Value == null)
fld.Value = Settings.Fields[fld].DefaulValue;
}*/
}
https://learn.microsoft.com/zh-tw/dotnet/api/system.xml.serialization.xmlattributes.xmldefaultvalue?view=netcore-3.1
here I google it . maybe try it?
Use default attribute : DefaultValueAttribute
public class Pet
{
// The default value for the Animal field is "Dog".
[DefaultValueAttribute("Dog")]
public string Animal ;
}
The Settings.settings xml file was designed for static project settings and, using user scoped settings, can be saved at runtime. Are you changing the settings so much that it no longer have the 'old' values or just adding to the list of settings?
If just adding, you don't need to loop through the settings one by one and try to guess their types with values as you can just do this:
int myInteger = Properties.Settings.Default.MyIntegerSettingValue;
And writing to the settings file:
Properties.Settings.Default.MyIntegerSettingValue = myInteger;
So if you cannot replace your settings.xml file, my suggestion is to model your settings to a class that contain all of your settings loaded at runtime and for each one missing, just write it out to the Settings file with your default value:
Properties.Settings.Default.MyMissingSetting = "MyDefaultValue"
You can find some nice info on application settings usage here
I need to access a specific property inside a COM object (the iTunes COM Library). You can access this property with the dynamic view of the Visual Studio debugger.
I tried to get this property using Reflection but I don't get any private properties or fields back.
I can access all the Properties that I also see in the debugger using this line:
new Microsoft.CSharp.RuntimeBinder.DynamicMetaObjectProviderDebugView(myObject).Items
However, I would rather not use this call because I believe an easier solution exists.
If you have iTunes installed this would be a simple example of what I'm trying to achieve:
iTunesAppClass app;
if (Process.GetProcessesByName("iTunes").Any())
{
app = new iTunesAppClass();
}
else
{
return;
}
foreach (IITPlaylist playlist in app.LibrarySource.Playlists)
{
// This does not work. There is no "Parent".
//var parent = playlist.Parent;
Type playListType = playlist.GetType();
// both contain 0 results
var fields = playListType.GetFields(BindingFlags.NonPublic);
var properties = playListType.GetFields(BindingFlags.NonPublic);
// works but only during runtime
//var parent2 = new Microsoft.CSharp.RuntimeBinder.DynamicMetaObjectProviderDebugView(playlist).Items[4];
}
What is the best way to access Microsoft Access Database object's Properties (like in CurrentDb.Properties) from C# in Visual Studio 2010?
(Not essential: In fact I want to get rid of Replication in a few dozen databases "on demand". Replication is not in use for a few years, and it was OK for MS Access prior to 2013. Access 2013 rejects databases with this feature.)
You can iterate over and modify the properties in the Access database by using the Access DAO object library.
The following code iterates over the properties in the database as well over the different containers and its properties as well over the Documents and its properties in the Databases container. The output is written to the Debug Output window.
After that I pick a Property from different property collections and change it's value. Do note that using the indexer on the collection will throw an exception if the property doesn't exist.
Make sure you have a reference to the Primary Interop Assembly for Microsoft Office 12.0 Access database engine Object Library (your version migth vary) so that you can have the following in your using statements:
using Microsoft.Office.Interop.Access.Dao;
Your method would go like this:
// Open a database
var dbe = new DBEngine();
var db = dbe.OpenDatabase(#"C:\full\path\to\your\db\scratch.accdb");
// Show database properties
DumpProperties(db.Properties);
// show all containers
foreach (Container c in db.Containers)
{
Debug.WriteLine("{0}:{1}", c.Name, c.Owner);
DumpProperties(c.Properties);
}
//Show documents and properties for a specific container
foreach (Document d in db.Containers["Databases"].Documents)
{
Debug.WriteLine("--------- " + d.Name);
DumpProperties(d.Properties);
}
// set a property on the Database
Property prop = db.
Properties["NavPane Width"];
prop.Value = 300;
// set a property on the UserDefined document
Property userdefProp = db
.Containers["Databases"]
.Documents["UserDefined"]
.Properties["ReplicateProject"];
userdefProp.Value = true;
Property dumper helper
private static void DumpProperties(Properties props)
{
foreach (Property p in props)
{
object val = null;
try
{
val = (object)p.Value;
}
catch (Exception e)
{
val = e.Message;
}
Debug.WriteLine(
"{0} ({2}) = {1}",
p.Name,
val,
(DataTypeEnum) p.Type);
}
}
I used this to overcome an exception being thrown on dynamic types (as the Value property turns out to be)
I apologize in advance for the long description of a simple question but I want to make sure people properly understand what I'm trying to do.
Background
I'm writing a tool that can read in a file generated by SqlMetal and create a class that contains methods for simple Inserting, Updating, Deleting and Selecting, which can then be exposed to a web service. The main advantage here is that if a table changes, I simply have to re-run the tool and the database-related code is automatically updated and everywhere that uses it will generate compile errors, making it easy to track down where manual changes need to be made. For example, if I have a Customer table that has the following fields:
CustomerId (PK, Identity)
FirstName
LastName
I want to be able to generate Insert and Delete methods as follows:
// I only want non-database-generated fields to be parameters here.
public static Customer InsertCustomer(String firstName, String lastName)
{
...
}
// I only want the primary key fields to be parameters here.
public static int DeleteCustomer(int customerId)
{
...
}
I am using SqlMetal to generate a Customer class. Now what I want to do is read that .cs file into my new tool in order to create another class with the above methods. This new class can then be exposed to the web service to grant access to this functionality without having to expose the underlying database. I am using NRefactory to read in the SqlMetal-generated file and so far, it's going well but I've run into a snag trying to read the property attributes on my Customer class.
SqlMetal generates its classes using a ColumnAttribute to identify each property that is derived from a database column. The ColumnAttribute will have a number of arguments to describe the database column's properties. In the above example, it would generate something like this:
...
[Column(Name="customerId", Storage="_CustomerId, DbType="INT NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
public int CustomerId
{
...
}
[Column(Name="firstName", Storage="_FirstName", DbType="NVarChar(100) NOT NULL", CanBeNull=false)]
public String FirstName
{
...
}
[Column(Name="lastName", Storage="_LastName", DbType="NVarChar(100) NOT NULL", CanBeNull=false)]
public String LastName
{
...
}
...
Problem
As you can see, SqlMetal gives me the attributes I need in order to identify which columns are database-generated and which ones are part of the primary key. So when I read this file into NRefactory and resolve the type, I would expect to be able to get at all of this information. However, I'm finding that while I can get to the ColumnAttribute, all of the arguments on it are unresolved and therefore aren't accessible via the NamedArguments or PositionalArguments properties.
Here's my code:
SyntaxTree syntaxTree = ...;
foreach(AstNode tableNode in syntaxTree.Children)
{
ResolveResult result = resolver.Resolve(tableNode);
var properties = result.Type.GetProperties();
foreach (IProperty p in properties)
{
var attributes = p.Attributes;
bool isPrimaryKeyField = false;
bool isDbGenerated = false;
bool isColumn = false;
foreach (IAttribute attr in attributes)
{
if (attr.AttributeType.Name == "Column")
{
isColumn = true;
foreach (var arg in attr.NamedArguments) // NamedArguments contains no items.
{
if (arg.Key.Name == "IsPrimaryKey")
{
isPrimaryKeyField = (bool)arg.Value.ConstantValue == true;
}
if (arg.Key.Name == "IsDbGenerated")
{
isDbGenerated = (bool)arg.Value.ConstantValue == true;
}
}
}
}
if (isColumn)
{
... // Create a parameter as appropriate.
}
}
}
This all works until I try to loop through the IAttribute.NamedArguments because the collection contains no elements. However, when I go through the debugger and examine the value of 'attr', I can see that there is a private variable called 'unresolved', which contains a list of all the arguments I want but I can find no way to access this through code.
How do I get at the contents of this 'unresolved' variable? Do I need to do something more with the Resolver? This is my first time using NRefactory so I'm not overly familiar with all the nuances yet. I've been having a tough time finding an example that goes into this level of depth on Google and the documentation I've seen for NRefactory doesn't seem to cover it. Any help would be appreciated.
I figured it out. I needed to load the assembly for System.Data.Linq into the IProjectContent before resolving the SyntaxTree.
...
CecilLoader loader = new CecilLoader();
Assembly[] assembliesToLoad = {
...
typeof(System.Data.Linq.Mapping.ColumnAttribute).Assembly
...};
IUnresolvedAssembly[] projectAssemblies = new IUnresolvedAssembly[assembliesToLoad.Length];
for(int i = 0; i < assembliesToLoad.Length; i++)
{
projectAssemblies[i] = loader.LoadAssemblyFile(assembliesToLoad[i].Location);
}
IProjectContent project = new CSharpProjectContent();
project = project.AddAssemblyReferences(projectAssemblies);
...
I'm using Enterprise Library 3.1 and want to programmatically access the Logging Block (runtime, object model) specifically its Trace Listeners and Sources.
For example, I want to access the Filename property of a trace listener object so I can know where the log file is located on disk.
Update: Looking for answers that use the runtime object model, not by parsing the XML configuration.
You can access the logging configuration programmatically using the object model (used for configuration).
To get the specific data for the trace listener you should look at TraceListenerData (and the specific subclasses).
This example shows how to read in the configuration and then get the TraceListeners:
// Open config file
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = #"MyApp.exe.config";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
// Get EL log settings
LoggingSettings log = config.GetSection("loggingConfiguration") as LoggingSettings;
// Get TraceListener info
foreach(TraceListenerData listener in log.TraceListeners)
{
// Check for listener types you care about
if (listener is RollingFlatFileTraceListenerData)
{
RollingFlatFileTraceListenerData data = listener as RollingFlatFileTraceListenerData;
Console.WriteLine(string.Format("Found RollingFlatFileLIstener with Name={0}, FileName={1}, Header={2}, Footer={3}, RollSizeKB={4}, TimeStampPattern={5},RollFileExistsBehavior={6}, RollInterval={7}, TraceOutputOptions={8}, Formatter={9}, Filter={10}",
data.Name, data.FileName, data.Header, data.Footer, data.RollSizeKB,
data.TimeStampPattern, data.RollFileExistsBehavior, data.RollInterval,
data.TraceOutputOptions, data.Formatter, data.Filter);
}
else // other trace listener types e.g. FlatFileTraceListenerData
{
}
}
Apparently some needed info is privately encapsulated in a LogWriterStructureHolder instance (its field is named structureHolder) on the Enterprise Library Logger.Writer instance (of Type LogWriter).
So I'm effectively looking for: Logger.Writer.structureHolder (but that field is private).
I used reflection to pull it out....
These are the significant namespaces:
using System.Reflection;
using Microsoft.Practices.EnterpriseLibrary.Logging;
This is reflection code to pull out the needed private data:
// Get the private field.
FieldInfo fiLogStructHolder
= typeof(LogWriter).GetField("structureHolder", BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic);
// Obtain field value to get the private data.
LogWriterStructureHolder structureHolder
= (LogWriterStructureHolder)fiLogStructHolder.GetValue(Logger.Writer);
// Access the value's .TraceSources property of Type Dictionary<string, LogSource>.
// The string is the name of the category from configuration.
int numSources = structureHolder.TraceSources.Count;
// Furthermore, access the listeners of any logging source by specifying:
int numListeners = structureHolder.TraceSources[0].Listeners.Count
// ^-- Note: Picked first source for example.
If anybody can find a non-private entry point for this same data please post it in an answer. Thanks.
Kudos to .NET Reflector for facilitating this answer.
public static EmailTraceListenerData GetEmailLogConfiguration()
{
var rootWebConfig1 = WebConfigurationManager.OpenWebConfiguration("/");
var section = rootWebConfig1.GetSection("loggingConfiguration");
var loggingSection = section as Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings;
if (loggingSection != null) {
// Reference to Microsoft.Practices.EnterpriseLibrary.Logging.dll and
// Microsoft.Practices.EnterpriseLibrary.Common.dll required for the code below
foreach (Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.TraceListenerData listener in loggingSection.TraceListeners) {
var emailTraceListenerData = listener as EmailTraceListenerData;
if (emailTraceListenerData != null) {
// Can obtain FromAddress, ToAddress, SmtpServer and SmtpPort
// as property of emailTraceListenerData;
return emailTraceListenerData;
}
}
}
return null;
}
Web.config file is as follow:
For Windows application, you can open the .config file withSystem.Configuration.ConfigurationManager.OpenExeConfigurationinstead of WebConfigurationManager.
The other answers seem very verbose, here is my solution:
public static TraceListenerData GetTraceListener(string name)
{
var log = ConfigurationManager.GetSection("loggingConfiguration") as LoggingSettings;
return log.TraceListeners.Single(tl => tl.Name == name);
}
Once you've called this helper method you can cast the result to whatever type the listener is such as RollingFlatFileTraceListenerData, EmailTraceListenerData, FormattedEventLogTraceListenerData, FormattedDatabaseTraceListenerData