0

I'm trying to work with OpenOffice Calc document under C# and getting an error "Url seems to be an unsupported one for network". My code is:

string filePath="serverName\\folder\\file.ods";
filePath = "file:///" + filePath.Replace(@"\", "/");

PropertyValue[] props = new PropertyValue[0];
XComponent oDoc = loader.loadComponentFromURL(AFile, "_blank", 0, props);

Error is raising only for files in network. When I'am using:

string filePath="C:\\folder\\file.ods";

Everything is ok. So I can't figure out how I should convert my path to be correct. Can anybody explain me?

Vertexwahn
  • 7,709
  • 6
  • 64
  • 90
Gleb
  • 1,412
  • 1
  • 23
  • 55

3 Answers3

1

I find out what was wrong. The problem was that file was in folder name looks like "UserName#Files". Actually the problem is symbol '#'. When I replace it with '%23'(encoding to url notation) everything works fine.

UPD:

Better way to use System.Uri to convert path:

string filePath="\\servername\folder\UserName#Files\file.ext";
Uri fileUri=new Uri(filePath);
filePath=fileUri.Absolute.Uri;
Gleb
  • 1,412
  • 1
  • 23
  • 55
  • As a side note, you should rather use System.IO.Path methods (Path.Combine, etc) and generally work with strongly-typed objects, just to prevent this kind of problem. – thomasb Aug 04 '15 at 13:27
0

Try to use:

string filePath="serverName\\folder\\file.ods";
filePath = "//" + filePath.Replace(@"\", "/");
Backs
  • 24,430
  • 5
  • 58
  • 85
0

I don't find any reason why you are using @ in the replace function, as Replace function replaces all \ to /

string filePath="serverName\\folder\\file.ods";
filePath = "file://" + filePath.Replace("\", "/");
Learner
  • 71
  • 13
  • I'm trying but this does not works to with same exception. – Gleb Aug 04 '15 at 10:55
  • 1
    Yes, tryed this. I'm using @ becouse \ - is escape sequence. – Gleb Aug 04 '15 at 11:02
  • I know it is an escape `sequence` but whats the purpose of having it in `Replace`. You have to pass "//" to give the path. I don't think it is required – Learner Aug 04 '15 at 11:10
  • Don't understand what your mean. Code you posted: filePath = "file://" + filePath.Replace("\", "/"); will not compile. – Gleb Aug 04 '15 at 11:23