How to open a file that is within the solution with Process.Start

Asked

Viewed 1,537 times

1

I added my solution in C# a folder called "files" inside this folder will be placed some standard documents that can be analyzed when registering some projects.

I have a combobox in the form for the person to choose the file you want to open.

I’m trying to use that code:

Process.Start("../arquivos/teste.jpg");

But of the error. It does not find the directory. I wonder if it has how to put the path this way using Process.Start.

  • 1

    how are you filling the Combobox with the files? What error do you get when running Process.Start(...)?

  • about this, it is right he is identifying the value and knows which is the selected file. The error is "File not found." Because the path is not complete(e.g.: C: Files test.jpg) only that I didn’t want it to be like this... I wanted it to be a way inside the solution so it could be done the way I asked the question

  • 2

    I can almost assure you that the address of the file is wrong even. I don’t see another problem. Although you might think you’re in one directory and you might be in another. Print Environment.CurrentDirectory to see if you are where you think you should be, otherwise you have to change or inform where you are. Or use the full path.

  • Yes vc is right at one point the current address is : (C: Users Lucas Documents Visual Studio 2012 Projects SLN_CAPRO CAPRO bin Debug) and what I want would be (C: Users Lucas Documents Visual Studio 2012 Projects SLN_CAPRO CAPRO test files.jpg) there’s some way I can open the file without having to go the whole way?

  • Give I’ll try to answer. But that hardly ask you, because you are trying to run an image? :)

  • The image is just a test, which will run a PDF with some design standards

  • Gave in the same :)

Show 2 more comments

1 answer

1


You see, if you want to make an application that works in all situations you need to make sure it works in all situations. If you don’t find the file you want it is likely that it doesn’t exist at all. At least not where you are looking.

I could show you how to access the desired directory in another way but I think your problem is quite simple. And another way does not guarantee that it will solve the problem.

You’re in :

C:\Users\Lucas\Documents\Visual Studio 2012\Projects\SLN_CAPRO\CAPRO\bin\Debug

when order access:

../arquivos/teste.jpg

means you’re accessing:

C:\Users\Lucas\Documents\Visual Studio 2012\Projects\SLN_CAPRO\CAPRO\bin\arquivos\teste.jpg

which is not what where you say the file is:

C:\Users\Lucas\Documents\Visual Studio 2012\Projects\SLN_CAPRO\CAPRO\arquivos\teste.jpg

Then the correct thing would be to access it through:

../../arquivos/teste.jpg

But you may want to set up a more complex execution environment with ProcessStartInfo:

var processo = Process.Start(new ProcessStartInfo() {
    WorkingDirectory = "../../arquivos",
    FileName = "teste.jpg"
});

I put in the Github for future reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.