0

I am using spire library to read a word file in winform in c#.

here is my code:

Document mydoc = new Document();
mydoc.LoadFromFile(@"C:\Users\user\documents\visual studio 2015\Projects\FormsPrint\FormsPrint\word\1.docx");

but this address is not relative. the problem is I can't move this app to other computers because of its absolute address.

I changed my code to:

Document mydoc = new Document();
mydoc.LoadFromFile(@"\word\1.docx");

but it didn't work again. I used ~/ at first of address but didn't work too. note that the function "loadfromfile" is one of spire library functions that gets a string path for input. how can I use relative path? I read other questions and answers like this but didn't work for me. how can I change this address to the relative address?

Ganesa Vijayakumar
  • 2,422
  • 5
  • 28
  • 41
Siamak Farsi
  • 37
  • 1
  • 8
  • `\`` is weird. Some things interpret it as the root of the current partition (e.g `C:\\\``), but some say it's not a valid character to start a path with. And some have weird behaviour. `Directory.Exists("\\")` returns true, but doing `Path.GetDirectoryName` returns null. Instead try using `.\`` or just starting without a `\``. **Edit** backslashes and comments are weird, expect wonky formatting – MindSwipe Nov 29 '19 at 12:15
  • Also, what does this question have to do with `spire` (the numeric library for Scala)? – MindSwipe Nov 29 '19 at 12:18
  • 2
    Are you running in Debug? If so remember that your executable is in bin\Debug and runs in that folder, so all paths have to be relative to this.. – Jonathan Willcock Nov 29 '19 at 12:19
  • 2
    Make sure you include your `word\1.docx` in your VS solution and then click on it and change `Copy to output directory` to `Copy always`. This way the file will be copied to wherever your executable is build and then run from. – Wiktor Zychla Nov 29 '19 at 12:22

1 Answers1

2

You may need to have the word folder same location where your .exe is:

enter image description here

then, call it as

mydoc.LoadFromFile(@"word\1.docx");
rjs123431
  • 688
  • 4
  • 14