Following this tutorial: http://www.codeproject.com/Articles/26528/C-Application-to-Watch-a-File-or-Directory-using-F
I've modified the above tutorial and created a windows form application that can create multiple filesystemwatcher instances and track/delete them in a list on the form.
My next goal is to make it so that on computer restart/startup, the windows form application will remain active as well as all the watcher objects I've created.
I figure that this would take 4 parts:
1) Save information on watcher objects and their metadata to be accessed after restart (currently I save references to all watcher objects in a List variable)
2) Have the watcher objects persist even after restart
3) Start the windows form on startup
4) Access the saved watcher object information so that it still appears on the windows form list.
I am new to C# and this is my first project, so I have little idea on how to tackle these steps. Any help would be appreciated.
You have to create new watcher objects after restart. The information you need to store needs to have a different class, e.g.:
public class SettingItem
{
public string Path { get; set; }
}
public class Settings
{
public SettingItem[] Items { get; set; }
}
Then you can use XmlSerializer to store the objects on disk:
public static class SettingManager
{
public static void Save(string path, object obj)
{
if (string.IsNullOrWhiteSpace(path))
throw new ArgumentNullException(nameof(path));
if (obj == null)
throw new ArgumentNullException(nameof(obj));
Directory.CreateDirectory(Path.GetDirectoryName(path));
using (var s = new StreamWriter(path))
{
var xmlSerializer = new XmlSerializer(obj.GetType());
xmlSerializer.Serialize(s, obj);
}
}
public static T Load<T>(string path) where T : class, new()
{
if (string.IsNullOrWhiteSpace(path))
throw new ArgumentNullException(nameof(path));
if (File.Exists(path))
{
using (var s = new StreamReader(path))
{
var xmlSerializer = new XmlSerializer(typeof(T));
return xmlSerializer.Deserialize(s) as T;
}
}
return new T();
}
}
Usage:
var settingPath = #"c:\...xml";
var settings0 = new Settings
{
Items = new[]
{
new SettingItem { Path = #"c:\path\to\watch" },
}
};
SettingManager.Save(settingPath, settings0);
var settings1 = SettingManager.Load<Settings>(settingPath);
Related
So I'm making a game, and it saves users' progress on the computer in a binary file. The User class stores a few things:
Integers for stat values (Serializable)
Strings for the Username and the skin assets
Lists of both the Achievement class and the InventoryItem class, which I have created myself.
Here are the User fields:
public string Username = "";
// ID is used for local identification, as usernames can be changed.
public int ID;
public int Coins = 0;
public List<Achievement> AchievementsCompleted = new List<Achievement>();
public List<InventoryItem> Inventory = new List<InventoryItem>();
public List<string> Skins = new List<string>();
public string CurrentSkinAsset { get; set; }
The Achievement class stores ints, bools, and strings, which are all serializable. The InventoryItem class stores its name (a string) and an InventoryAction, which is a delegate that is called when the item is used.
These are the Achievement class's fields:
public int ID = 0;
public string Name = "";
public bool Earned = false;
public string Description = "";
public string Image;
public AchievmentDifficulty Difficulty;
public int CoinsOnCompletion = 0;
public AchievementMethod OnCompletion;
public AchievementCriteria CompletionCriteria;
public bool Completed = false;
And here are the fields for the InventoryItem class:
InventoryAction actionWhenUsed;
public string Name;
public string AssetName;
The source of the InventoryAction variables are in my XNAGame class. What I mean by this is that the XNAGame class has a method called "UseSword()" or whatever, which it passes into the InventoryItem class. Previously, the methods were stored in the Game1 class, but the Game class, which Game1 inherits from, is not serializable, and there's no way for me to control that. This is why I have an XNAGame class.
I get an error when trying to serialize: "The 'SpriteFont' class is not marked as serializable", or something like that. Well, there is a SpriteFont object in my XNAGame class, and some quick tests showed that this is the source of the issue. Well, I have no control over whether or not the SpriteFont class is Serializable.
Why is the game doing this? Why must all the fields in the XNAGame class be serializable, when all I need is a few methods?
Keep in mind when answering that I'm 13, and may not understand all the terms you're using. If you need any code samples, I'll be glad to provide them for you. Thanks in advance!
EDIT: One solution I have thought of is to store the InventoryAction delegates in a Dictionary, except that this will be a pain and isn't very good programming practice. If this is the only way, I'll accept it, though (Honestly at this point I think this is the best solution).
EDIT 2: Here's the code for the User.Serialize method (I know what I'm doing in inefficient, and I should use a database, blah, blah, blah. I'm fine with what I'm doing now, so bear with me.):
FileStream fileStream = null;
List<User> users;
BinaryFormatter binaryFormatter = new BinaryFormatter();
try
{
if (File.Exists(FILE_PATH) && !IsFileLocked(FILE_PATH))
{
fileStream = File.Open(FILE_PATH, FileMode.Open);
users = (List<User>)binaryFormatter.Deserialize(fileStream);
}
else
{
fileStream = File.Create(FILE_PATH);
users = new List<User>();
}
for (int i = 0; i < users.Count; i++)
{
if (users[i].ID == this.ID)
{
users.Remove(users[i]);
}
}
foreach (Achievement a in AchievementsCompleted)
{
if (a.CompletionCriteria != null)
{
a.CompletionCriteria = null;
}
if (a.OnCompletion != null)
{
a.OnCompletion = null;
}
}
users.Add(this);
fileStream.Position = 0;
binaryFormatter.Serialize(fileStream, users);
You cannot serialize a SpriteFont by design, actually this is possible (.XNB file) but it hasn't been made public.
Solution:
Strip it off your serialized class.
Alternatives:
If for some reasons you must serialize some font, the first thing that comes to my mind would be to roll-out your own font system such as BMFont but that's a daunting task since you'll have to use it everywhere else where you might already do ...
Generate a pre-defined amount of fonts (i.e. Arial/Times/Courier at size 10/11/12 etc ...) using XNA Content app (can't recall its exact name); then store this user preference as two strings. With a string.Format(...) you should be able to load the right font back quite easily.
Alternative 2 is certainly the easiest and won't take more than a few minutes to roll-out.
EDIT
Basically, instead of saving a delegate I do the following:
inventory items have their own type
each type name is de/serialized accordingly
their logic does not happen in the main game class anymore
you don't have to manually match item type / action method
So while you'll end up with more classes, you have concerns separated and you can keep your main loop clean and relatively generic.
Code:
public static class Demo
{
public static void DemoCode()
{
// create new profile
var profile = new UserProfile
{
Name = "Bill",
Gold = 1000000,
Achievements = new List<Achievement>(new[]
{
Achievement.Warrior
}),
Inventory = new Inventory(new[]
{
new FireSpell()
})
};
// save it
using (var stream = File.Create("profile.bin"))
{
var formatter = new BinaryFormatter();
formatter.Serialize(stream, profile);
}
// load it
using (var stream = File.OpenRead("profile.bin"))
{
var formatter = new BinaryFormatter();
var deserialize = formatter.Deserialize(stream);
var userProfile = (UserProfile) deserialize;
// set everything on fire :)
var fireSpell = userProfile.Inventory.Items.OfType<FireSpell>().FirstOrDefault();
if (fireSpell != null) fireSpell.Execute("whatever");
}
}
}
[Serializable]
public sealed class UserProfile
{
public string Name { get; set; }
public int Gold { get; set; }
public List<Achievement> Achievements { get; set; }
public Inventory Inventory { get; set; }
}
public enum Achievement
{
Warrior
}
[Serializable]
public sealed class Inventory : ISerializable
{
public Inventory() // for serialization
{
}
public Inventory(SerializationInfo info, StreamingContext context) // for serialization
{
var value = (string) info.GetValue("Items", typeof(string));
var strings = value.Split(';');
var items = strings.Select(s =>
{
var type = Type.GetType(s);
if (type == null) throw new ArgumentNullException(nameof(type));
var instance = Activator.CreateInstance(type);
var item = instance as InventoryItem;
return item;
}).ToArray();
Items = new List<InventoryItem>(items);
}
public Inventory(IEnumerable<InventoryItem> items)
{
if (items == null) throw new ArgumentNullException(nameof(items));
Items = new List<InventoryItem>(items);
}
public List<InventoryItem> Items { get; }
#region ISerializable Members
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
var strings = Items.Select(s => s.GetType().AssemblyQualifiedName).ToArray();
var value = string.Join(";", strings);
info.AddValue("Items", value);
}
#endregion
}
public abstract class InventoryItem
{
public abstract void Execute(params object[] objects);
}
public abstract class Spell : InventoryItem
{
}
public sealed class FireSpell : Spell
{
public override void Execute(params object[] objects)
{
// using 'params object[]' a simple and generic way to pass things if any, i.e.
// var world = objects[0];
// var strength = objects[1];
// now do something with these !
}
}
Okay, so I figured it out.
The best solution was to use a Dictionary in the XNAGame class, which stores two things: an ItemType (an enumeration), and an InventoryAction. Basically, when I use an item, I check it's type and then look up it's method. Thanks to everyone who tried, and I'm sorry if the question was confusing.
I'm new to MEF. I'm wondering when to initialize instances through composition rather than calling their constructor. For example I'm not sure how to approach the following problem in MEF. If Manager class is in a another DLL and referenced in Program
class Program
{
static void Main(string[] args)
{
var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
var container = new CompositionContainer(catalog);
var manager= container.GetExportedValue<Manager>();
container.ComposeParts(manager);
var letter = manager.Letter;
Console.ReadKey();
}
}
//Manager Class is Business Access Layer referenced to Program as DLL
[Export]
public class Manager
{
private Letter _letter;
[Import]
public Letter Letter
{
get { return _letter ?? (_letter = InitializeComposeLetter()); }
}
private Letter InitializeComposeLetter()
{
var attachments = new List<string>();
var details = new StringBuilder();
var attachmentDirectory = ConfigurationManager.AppSettings["attachmentsDirectory"];
var letterPath = ConfigurationManager.AppSettings["letterPath"];
if (Directory.Exists(attachmentDirectory))
{
var files = Directory.EnumerateFiles(attachmentDirectory);
attachments = files.ToList();
}
if (File.Exists(letterPath))
{
var lines = File.ReadAllLines(letterPath);
foreach (var line in lines)
{
details.Append(line);
details.Append(Environment.NewLine);
}
}
//*********************QUESTION IS HERE *************//
var letter = new Letter() //SHOULD I MEF THIS OUT ??? IF SO HOW??
{
Subject = ConfigurationManager.AppSettings["defaultEmailTitle"],
Details = details.ToString(),
Attachments = attachments,
};
return letter;
}
}
Since you are not using auto-implemented properties, you should import the field instead of the property:
[Import]
private Letter _letter;
Note that if there is not part of type Letter, the composition of Manager will fail. If you want to handle it yourself (as is shown from your code sample) you can use the ImportAttribute.AllowDefault property:
[Import(AllowDefault = true)]
private Letter _letter;
Finally you do not need the call to ComposeParts. This is needed for objects that you have created. If the object is created by the container the composition is done behind the scenes.
So for objects that you create:
var manager = new Manager();
container.ComposeParts(manager);
otherwise:
var manager= container.GetExportedValue<Manager>();
Note that GetExportedValue will throw if the composition fails. There are other GetExportXXX methods that will not throw. Choose one depending on your needs.
I am having trouble figuring out how to write this collection out to file. I have the following classes
public static class GeoPolyLines
{
public static ObservableCollection<Connections> connections = new ObservableCollection<Connections>();
}
public class Connections
{
public IEnumerable<IEnumerable<Point>> Points { get; set; }
public Connections(Point p1, Point p2)
{
Points = new List<List<Point>>
{
new List<Point>
{
p1, p2
}
};
}
}
And then a bunch of things like this:
GeoPolyLines.connections.Add(new Connections(new Point(GeoLocations.locations[0].Longitude, GeoLocations.locations[0].Latitude), new Point(GeoLocations.locations[1].Longitude, GeoLocations.locations[1].Latitude)));
So GeoPolyLines.connections will eventually have a bunch of different locations that I want to then write out to a .txt file to save and reload if I need to. But I don't know how to do this. I have something like this:
using (StreamWriter sw = new StreamWriter(filename))
{
var enumerator = GeoPolyLines.connections.GetEnumerator();
while (enumerator.MoveNext())
{
}
sw.Close();
}
use serialization.
To write to file
var serializer = new JavaScriptSerializer();
File.WriteAllText(filename, serializer.Serialize(points));
and to read from file
var points = serializer.Deserialize<List<Point>>(File.ReadAllText(filename));
I'm currently trying out the new MemoryCache in .Net 4 to cache a few bits of data in one of our apps. The trouble I have is the objects are updated and the cache appears to be persisting the changes e.g.
public IEnumerable<SomeObject> GetFromDatabase(){
const string _cacheKeyGetDisplayTree = "SomeKey";
ObjectCache _cache = MemoryCache.Default;
var objectInCache = _cache.Get(_cacheKeyGetDisplayTree) as IEnumerable<SomeObject>;
if (objectInCache != null)
return objectInCache.ToList();
// Do something to get the items
_cache.Add(_cacheKeyGetDisplayTree, categories, new DateTimeOffset(DateTime.UtcNow.AddHours(1)));
return categories.ToList();
}
public IEnumerable<SomeObject> GetWithIndentation(){
var categories = GetFromDatabase();
foreach (var c in categories)
{
c.Name = "-" + c.Name;
}
return categories;
}
If I were calling GetWithIndentation() first and then later calling GetFromDatabase() I would expect it to return the original list of SomeObject but instead it returns the modified items (with "-" prefixed on the name).
I thought ToList() destroyed the reference but it still seems to persist the changes. I'm sure it's obvious but can anyone spot where I'm going wrong?
I created a ReadonlyMemoryCache class to solve this problem. It inherits from the .NET 4.0 MemoryCache, but objects are stored readonly (by-value) and cannot be modified. I deep copy the objects before storing using binary serialization.
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Runtime.Caching;
using System.Runtime.Serialization.Formatters.Binary;
using System.Threading.Tasks;
namespace ReadOnlyCache
{
class Program
{
static void Main()
{
Start();
Console.ReadLine();
}
private static async void Start() {
while (true)
{
TestMemoryCache();
await Task.Delay(TimeSpan.FromSeconds(1));
}
}
private static void TestMemoryCache() {
List<Item> items = null;
string cacheIdentifier = "items";
var cache = ReadonlyMemoryCache.Default;
//change to MemoryCache to understand the problem
//var cache = MemoryCache.Default;
if (cache.Contains(cacheIdentifier))
{
items = cache.Get(cacheIdentifier) as List<Item>;
Console.WriteLine("Got {0} items from cache: {1}", items.Count, string.Join(", ", items));
//modify after getting from cache, cached items will remain unchanged
items[0].Value = DateTime.Now.Millisecond.ToString();
}
if (items == null)
{
items = new List<Item>() { new Item() { Value = "Steve" }, new Item() { Value = "Lisa" }, new Item() { Value = "Bob" } };
Console.WriteLine("Reading {0} items from disk and caching", items.Count);
//cache for x seconds
var policy = new CacheItemPolicy() { AbsoluteExpiration = new DateTimeOffset(DateTime.Now.AddSeconds(5)) };
cache.Add(cacheIdentifier, items, policy);
//modify after writing to cache, cached items will remain unchanged
items[1].Value = DateTime.Now.Millisecond.ToString();
}
}
}
//cached items must be serializable
[Serializable]
class Item {
public string Value { get; set; }
public override string ToString() { return Value; }
}
/// <summary>
/// Readonly version of MemoryCache. Objects will always be returned in-value, via a deep copy.
/// Objects requrements: [Serializable] and sometimes have a deserialization constructor (see http://stackoverflow.com/a/5017346/2440)
/// </summary>
public class ReadonlyMemoryCache : MemoryCache
{
public ReadonlyMemoryCache(string name, NameValueCollection config = null) : base(name, config) {
}
private static ReadonlyMemoryCache def = new ReadonlyMemoryCache("readonlydefault");
public new static ReadonlyMemoryCache Default {
get
{
if (def == null)
def = new ReadonlyMemoryCache("readonlydefault");
return def;
}
}
//we must run deepcopy when adding, otherwise items can be changed after the add() but before the get()
public new bool Add(CacheItem item, CacheItemPolicy policy)
{
return base.Add(item.DeepCopy(), policy);
}
public new object AddOrGetExisting(string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)
{
return base.AddOrGetExisting(key, value.DeepCopy(), absoluteExpiration, regionName);
}
public new CacheItem AddOrGetExisting(CacheItem item, CacheItemPolicy policy)
{
return base.AddOrGetExisting(item.DeepCopy(), policy);
}
public new object AddOrGetExisting(string key, object value, CacheItemPolicy policy, string regionName = null)
{
return base.AddOrGetExisting(key, value.DeepCopy(), policy, regionName);
}
//methods from ObjectCache
public new bool Add(string key, object value, DateTimeOffset absoluteExpiration, string regionName = null)
{
return base.Add(key, value.DeepCopy(), absoluteExpiration, regionName);
}
public new bool Add(string key, object value, CacheItemPolicy policy, string regionName = null)
{
return base.Add(key, value.DeepCopy(), policy, regionName);
}
//for unknown reasons, we also need deepcopy when GETTING values, even though we run deepcopy on all (??) set methods.
public new object Get(string key, string regionName = null)
{
var item = base.Get(key, regionName);
return item.DeepCopy();
}
public new CacheItem GetCacheItem(string key, string regionName = null)
{
var item = base.GetCacheItem(key, regionName);
return item.DeepCopy();
}
}
public static class DeepCopyExtentionMethods
{
/// <summary>
/// Creates a deep copy of an object. Must be [Serializable] and sometimes have a deserialization constructor (see http://stackoverflow.com/a/5017346/2440)
/// </summary>
public static T DeepCopy<T>(this T obj)
{
using (var ms = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(ms, obj);
ms.Position = 0;
return (T)formatter.Deserialize(ms);
}
}
}
}
In memory cached objects are stored within the same process space as the cache client process. When a cache client requests a cached object, the client receives a reference to the locally cached object rather than a copy.
The only way to get a clean copy of the object is to implement a custom clone mechanism (ICloneable, Serialization, Automapping, ...). With that copy you will be able to alter the new object without altering the parent object.
Depending on your use case, it is generally not recommanded to update an object in the cache.
You can do it easier if you deserialize and serialize again and get your cache object "By Value".
You can do it like this with Newtonsoft lib (just get it from NuGet)
var cacheObj = HttpRuntime.Cache.Get(CACHEKEY);
var json = JsonConvert.SerializeObject(cacheObj);
var byValueObj = JsonConvert.DeserializeObject<List<string>>(json);
return byValueObj;
Why not just store as json or a string? These are not passed by reference and when you get out of the cache you will get a new copy :) I am here to be challenged as thats what I am doing atm!
Serialization/Deserialization will solve the problem but at the same time it defeats the porpose of having objects in memory. The role of cache is to provide fast access to the stored object and we are adding the deserialization overhead here. Since deserialization is required I would suggest cache as service , something like redis cache, it will be centralized so you don't have to have copy of memory object per worker process and deserialization is anyways done.
The key thing in this case that you chose a fast serialization/deserialization option.
Windows Phone 7 App
The Goal of the application is a simple To Do list.
I have a class 'toditem' i add those objects to the Items object.
it seems to me I'm doing something really complicated and most likely no clean or decent code
But i have some serious problems with "IsolatedStorageFile"
public class ToDoItem
{
public string ToDoName { get; set; } // Add controle's enz.
public string ToDoDescription { get; set; }
internal Priority PriortiySelection { get; set; }
...
}
Items class (basicly a wrapper clas so i can acces it)
public class Items
{
public static List<ToDoItem> Itemslist = new List<ToDoItem>();
public static List<ToDoItem> GetList()
static methods here..
}
The code Belows returns the following exceptions :
"Attempt to access the method failed:
System.Io.streamreader..ctor
(System.String)"
and afterwards i get
Operation not permitted on IsolatedStorageFileSTream
if (store.FileExists(#"items.std"))
{
ToDoItem item = new ToDoItem();
try
{
IsolatedStorageFileStream save = new IsolatedStorageFileStream(#"items.std", FileMode.Open, store);
BinaryReader reader = new BinaryReader(save);
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
in public partial class NewToDo : PhoneApplicationPage
i added the following method. wich returns the above exceptions again i only assume that its allowd for some reason or i make some huge mistakes.
private void saveItem(ToDoItem toDoItem)
{
try
{
using (StreamWriter sw = new StreamWriter(store.OpenFile(#"items.std", FileMode.Append)))
{
sw.WriteLine(toDoItem.ToDoName);
sw.WriteLine(toDoItem.ToDoDescription);
sw.WriteLine(toDoItem.PriortiySelection.ToString());
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
Should u need more information I'm always glad to provide it, I'm currently a student at a Belgium college second year and I'm playing around with windows phone7 apps.
The following will read the contents of a file from isolated storage
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!store.FileExists(VIEW_MODEL_STORAGE_FILE))
{
return result;
}
using (var isfs = new IsolatedStorageFileStream(VIEW_MODEL_STORAGE_FILE, FileMode.Open, store))
{
using (var sr = new StreamReader(isfs))
{
string lineOfData;
while ((lineOfData = sr.ReadLine()) != null)
{
result += lineOfData;
}
}
}
}
The example builds a string of data (result). This is actually a serialized object which is actually a collection of other objects. This can then be deserialized back to the collection. This is probably preferable to what you were trying to do with writing out properties to a file one at a time.
Here's how to write the file:
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var isfs = new IsolatedStorageFileStream(VIEW_MODEL_STORAGE_FILE, FileMode.Create, store))
{
using (var sw = new StreamWriter(isfs))
{
sw.Write(serializedCollectionObject);
sw.Close();
}
}
}
Is it possible you're not disposing all your disposable objects and encountering a problem when you try to access a resource for a second time because it's still in use?
The using statement is a good way to handle this easily, more on that here.
Dispose with Using
A bit more background on the topic here where Jm47 was getting the same error message for this reason.
Problem opening a stream to an isolatedstorage image already the source on an image?