Handle a file ref, with backslashes, in a .resx, on a non-windows system
authorAnkit Jain <ankit.jain@xamarin.com>
Sat, 11 Mar 2017 02:38:04 +0000 (21:38 -0500)
committerMarek Safar <marek.safar@gmail.com>
Sat, 11 Mar 2017 10:03:07 +0000 (11:03 +0100)
File ref of the form:

```xml
  <data name="foo" type="System.Resources.ResXFileRef, System.Windows.Forms">
    <value>..\Resources\foo.json;System.Byte[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
  </data>
```

mcs/class/System.Windows.Forms/System.Resources/ResXFileRef.cs
mcs/class/System.Windows.Forms/Test/System.Resources/ResXFileRefTest.cs

index ca129da51cae0419e9f303decfb84d8fe50511ba..5200d86426a514927bce318bda44ae3d77d0c7c4 100644 (file)
@@ -82,7 +82,11 @@ namespace System.Resources {
                                        }
                                }
 
-                               using (FileStream file = new FileStream (parts [0], FileMode.Open, FileAccess.Read, FileShare.Read)) {
+                               string filename = parts [0];
+                               if (Path.DirectorySeparatorChar == '/')
+                                       filename = filename.Replace ("\\", "/");
+
+                               using (FileStream file = new FileStream (filename, FileMode.Open, FileAccess.Read, FileShare.Read)) {
                                        buffer = new byte [file.Length];
                                        file.Read(buffer, 0, (int) file.Length);
                                }
@@ -90,7 +94,7 @@ namespace System.Resources {
                                if (type == typeof(System.Byte[]))
                                        return buffer;
 
-                               if (type == typeof (Bitmap) && Path.GetExtension (parts [0]) == ".ico") {
+                               if (type == typeof (Bitmap) && Path.GetExtension (filename) == ".ico") {
                                        MemoryStream ms = new MemoryStream (buffer);
                                        return new Icon (ms).ToBitmap ();
                                }
index b0d1c924b86d4efcb659cac59529d26c2e06ffcf..83aef31a3b5232a2375b26764ee00e7a8ac4a1e7 100644 (file)
@@ -229,6 +229,26 @@ namespace MonoTests.System.Resources
                        }
                }
 
+               [Test]
+               public void ConvertFrom_Type_String_FilePathWithBackslashes ()
+               {
+                       if (Path.DirectorySeparatorChar == '\\')
+                               // non-windows test
+                               return;
+
+                       string fileContents = "foobar";
+                       string fileName = "foo.txt";
+                       string filePath = Path.Combine (_tempDirectory, fileName);
+                       File.WriteAllText (filePath, fileContents);
+
+                       filePath = _tempDirectory + "\\.\\" + filename;
+
+                       string fileRef = filePath + ";" + typeof (string).AssemblyQualifiedName;
+                       string result = _converter.ConvertFrom (fileRef) as string;
+                       Assert.IsNotNull (result, "#A1");
+                       Assert.AreEqual (result, fileContents, "#A2");
+               }
+
                [Test]
                public void ConvertFrom_Type_StreamReader ()
                {