how to fix System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified in C#

Dineshkumar.S 456 Reputation points
2023-01-20T06:13:12.1466667+00:00

I am trying to open a document in my windows application developed in C#. The document is kept inside the help option (menu strip) of the windows application. It works and opens fine in local but in the live version of the application, it is showing the error as mentioned below. Please suggest me any ideas for it. Thanks in advance

Unhandled error:

System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified

 

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,922 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,106 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,297 questions
{count} votes

Accepted answer
  1. Jack J Jun 24,796 Reputation points Microsoft Vendor
    2023-01-20T09:25:43.14+00:00

    @Dineshkumar.S , thanks for the feedback, based on my test, I reproduced your problem.

    Because the path will change when you published the app.

    You could use the property ApplicationDeployment.IsNetworkDeployed to check if your app has been published by clickonce.

    Please refer to the following steps to use the file when your app is published.

    First, please create a folder and add the file into the folder, like the following:

    User's image

    Second, please include the txt file in the Application Files:

    User's image

    Third, you could use the following code to call that file:

    private void button1_Click(object sender, EventArgs e)
            {
                if(ApplicationDeployment.IsNetworkDeployed)
                {
                    string path = AppDomain.CurrentDomain.BaseDirectory + @"\Data\1.txt";
                    string text = File.ReadAllText(path);
                    MessageBox.Show(text);
                }
                else
                {
                    string path = Path.Combine(Environment.CurrentDirectory, "..\\..") + @"\Data\1.txt";
                    string text = File.ReadAllText(path);
                    MessageBox.Show(text);
                }
       
    
            }
    

    Hope my solution could help you.

    Best Regards,

    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.