Search code examples
c#winformsrelative-pathspire

how to use relative address to read a file in C# winform


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?


Solution

  • 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");