diff --git a/examples/csharp/RouteGuide/RouteGuide/RouteGuide.csproj b/examples/csharp/RouteGuide/RouteGuide/RouteGuide.csproj index bd374a887c1..712df6150aa 100644 --- a/examples/csharp/RouteGuide/RouteGuide/RouteGuide.csproj +++ b/examples/csharp/RouteGuide/RouteGuide/RouteGuide.csproj @@ -16,7 +16,7 @@ - + diff --git a/examples/csharp/RouteGuide/RouteGuide/RouteGuideUtil.cs b/examples/csharp/RouteGuide/RouteGuide/RouteGuideUtil.cs index 96bd8ca09b9..29cd6b9bf9a 100644 --- a/examples/csharp/RouteGuide/RouteGuide/RouteGuideUtil.cs +++ b/examples/csharp/RouteGuide/RouteGuide/RouteGuideUtil.cs @@ -18,6 +18,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Reflection; using System.Text; using System.Threading.Tasks; @@ -28,7 +29,7 @@ namespace Routeguide /// public static class RouteGuideUtil { - public const string DefaultFeaturesFile = "route_guide_db.json"; + public const string DefaultFeaturesResourceName = "RouteGuide.route_guide_db.json"; private const double CoordFactor = 1e7; @@ -90,12 +91,12 @@ namespace Routeguide } /// - /// Parses features from a JSON file. + /// Parses features from an embedded resource. /// - public static List ParseFeatures(string filename) + public static List LoadFeatures() { var features = new List(); - var jsonFeatures = JsonConvert.DeserializeObject>(File.ReadAllText(filename)); + var jsonFeatures = JsonConvert.DeserializeObject>(ReadFeaturesFromResource()); foreach(var jsonFeature in jsonFeatures) { @@ -108,6 +109,19 @@ namespace Routeguide return features; } + private static string ReadFeaturesFromResource() + { + var stream = typeof(RouteGuideUtil).GetTypeInfo().Assembly.GetManifestResourceStream(DefaultFeaturesResourceName); + if (stream == null) + { + throw new IOException(string.Format("Error loading the embedded resource \"{0}\"", DefaultFeaturesResourceName)); + } + using (var streamReader = new StreamReader(stream)) + { + return streamReader.ReadToEnd(); + } + } + #pragma warning disable 0649 // Suppresses "Field 'x' is never assigned to". private class JsonFeature { diff --git a/examples/csharp/RouteGuide/RouteGuideClient/Program.cs b/examples/csharp/RouteGuide/RouteGuideClient/Program.cs index 9ce0cbcad3a..accaf0a0ec9 100644 --- a/examples/csharp/RouteGuide/RouteGuideClient/Program.cs +++ b/examples/csharp/RouteGuide/RouteGuideClient/Program.cs @@ -228,7 +228,7 @@ namespace Routeguide client.ListFeatures(400000000, -750000000, 420000000, -730000000).Wait(); // Record a few randomly selected points from the features file. - client.RecordRoute(RouteGuideUtil.ParseFeatures(RouteGuideUtil.DefaultFeaturesFile), 10).Wait(); + client.RecordRoute(RouteGuideUtil.LoadFeatures(), 10).Wait(); // Send and receive some notes. client.RouteChat().Wait(); diff --git a/examples/csharp/RouteGuide/RouteGuideServer/Program.cs b/examples/csharp/RouteGuide/RouteGuideServer/Program.cs index 4548ddf8e73..a79f8a1a28f 100644 --- a/examples/csharp/RouteGuide/RouteGuideServer/Program.cs +++ b/examples/csharp/RouteGuide/RouteGuideServer/Program.cs @@ -27,7 +27,7 @@ namespace Routeguide { const int Port = 50052; - var features = RouteGuideUtil.ParseFeatures(RouteGuideUtil.DefaultFeaturesFile); + var features = RouteGuideUtil.LoadFeatures(); Server server = new Server {