none
How to properly add resources to my project RRS feed

  • Question

  • Hello friends! 

    I am currently developing my first app and I am looking for some help. I am using the Microsoft Visual Studio and C# as my programming language. 

    Here is how the app is supposed to work: Users can make different selections using combo boxes and then click the 'Search' button to get what they're looking for.  Depending on their choices, the program is supposed to open a corresponding MS Word file with the details. To do this, I am using the NuGet package docX (by Xceed). This lets the app manipulate MS Word files (I.e. find the correct Word file, find the correct Heading in that file, create a new Word document, paste the required information into the new file and display it for the user). 

    This works fine when the program is ran from the Microsoft Visual Studio, where it was created. As soon as I publish my app and try to run it independently, I get an error message saying that the Word file could not be found. 

    I understand why I am getting this error. When I set it up, my app is unable to run code such as: 

    if (File.Exists(@"C:\Users\Dusan\source\repos\DZ Search\DZ Search\bin\Debug\procedura.docx"))
                {
                    
                    File.Delete(@"C:\Users\Dusan\source\repos\DZ Search\DZ Search\bin\Debug\procedura.docx");
                }
    
    System.Diagnostics.Process.Start(@"C:\Users\Dusan\source\repos\DZ Search\DZ Search\bin\Debug\procedura.docx"); 

    This is my first question: How can I set a specific file path where the app will retrieve the document from? Ideally, it should be in the app's installation directory, so it can work on anyone's PC.

    I would also like the docX package to work the same way. So far, it can only find/create documents that are located directly in the project folder. Is there any way to specify where the docX package will look for files, or where a new file will be created? 

    What I am trying to ask, how can I set up a database my program can reference to get all of the resources? 

    I really hope this makes sense to anyone reading this, because this is my very first project and I don't have a lot of experience using Microsoft Visual or C#. 

    Saturday, September 28, 2019 5:28 PM

All replies

  • Hello,

    Seems like you are looking in the executable path, this may or may not work dependent on how the app is installed e.g. ClickOnce or another method.

    Using statements

    using System.Diagnostics;
    using System.IO;

    var fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "procedura.docx");
    if (File.Exists(fileName))
    {
        File.Delete(fileName);
    }

    Same for starting opening the file

    Process.Start(fileName);

    Another option which requires System.Reflection in the using statements.

    var fileName = Path.Combine(Assembly.GetExecutingAssembly().Location, "procedura.docx");


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Saturday, September 28, 2019 5:47 PM
    Moderator
  • Hi DZ Creator,

    Thank you for posting here.

    >>This works fine when the program is ran from the Microsoft Visual Studio, where it was created. As soon as I publish my app and try to run it independently, I get an error message saying that the Word file could not be found.

    I think it is not related that if you publish the code. Because you have deleted the docx file according to the code you provided.

    If you want to add docx package to your project, you could refer to the following steos.

    First, right click reference -> manage NuGet Package.

    Second, search Docx in the search box.

    Finally, choose the Docx and install it.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, September 30, 2019 3:22 AM
    Moderator
  • Hello Karen,

     

    Thanks for the lines of code! They are very helpful!

    But I am still getting the following error: File could not be found. I think I know why I am getting this error. When the program is installed on another user’s computer, the word file the program needs is not there and can’t be opened. The word file was not added to my app. I need help properly adding a resource, or in this case, a word file, to my project. 

    Saturday, October 5, 2019 7:34 AM
  • Hello Jack,

    Thanks for taking the time to help me out... I appreciate it, but I already did that.

    Regards,

    DZ Creator

    Saturday, October 5, 2019 7:36 AM