Resolved Open file

Mimi

Member
Joined
Jul 13, 2020
Messages
9
Programming Experience
Beginner
Hi
I need to show the files in a specific folder on the disk in Winform. I solved it by displaying them in a listbox. They may also be displayed in other ways. I need to open these files. These files are jpg, jpeg, bmp, pdf, doc, docx, xls, xlsx. Is there any way to do this?
 
Hello, and welcome to the forums.

What have you tried or thought of?

Have you tried searching for answers first?

This is one of them questions which has been asked thousands of times already. Unless you have code to show, and code to report a problem with, there is little more we can do to help you. Perhaps if you show what code you have, maybe we can start giving you some pointers... When you come to a support forum, its generally a good start to show us what you have tried so we have something to work with.
 
Perhaps I'm missing something. Process.Start() will start a different process to show the contents of the files. I thought that you needed to show the contents of the files within your WinForms program. Or were you just unclear about what you really want to do? If you didn't need to display the initial list of files in a particular folder in WinForms then why not also just use Process.Start() to launch Explorer.exe set to open on that particular folder?
 
Hello
I need to view the contents of the folder. So far I have solved it with the listbox as follows:

C#:
string path = @"c:\Skuska\";

        private void Subory_add_Load(object sender, EventArgs e)
        {
 
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            listBox1.Items.Clear();
            string[] files = Directory.GetFiles(path);
            foreach (string file in files)
            {
                listBox1.Items.Add(file);
            }

Now I need it to open after clicking on the file. It will definitely work with Process.Start (). I still need to work out that after clicking it saves the file name and extension to a variable and then I open it with Process.Start ().
 
Yes, but how will make that WinForms? As far as I know Excel is not written in WinForms, so just launching Excel will not present the contents of the Excel document in WinForms.
 
@Mimi - can you give your response on this question :
I thought that you needed to show the contents of the files within your WinForms program. Or were you just unclear about what you really want to do?
The reason i suggested the process start is because I assumed it was what you wanted. I also didn't think you wanted to open the file in your actual form. To have the file open in your form, then this is a question way to broad to answer. As there are to many angles to cover, and each file would require multiple topics for each file you want to open. If you are happy to let the default application open that file, and not have it open within your form, then process start will do.

Specifically, with excel files, if you want to open them in winforms, you need to reference this assembly, and you can easily search up how to do that. Microsoft.Office.Interop.Excel
 
Hello
I need to open the files in the application that is associated with the extension. (.docx > Word, .txt > notepad ...) Using Process.Start I can already open it. I would need to find associated applications to extensions in Windows. It can be found in c # ?
 
You'd need to have an appropriate application installed on the computer for those file types to use process start effectively. As I already stated, you would need to use the interop for excel if you want to open the files and show them inside your application.

Your reply doesn't clearly answer the needed question. Should your application (preview the files internally in your application) or (externally in the default program from that file type) ???

It's one thing to open files from your app, its completely different if you want to view those file inside your app, so which is it? Just looking for clearer clarity please.
 
Hello
I need to open the files in the application that is associated with the extension. (.docx > Word, .txt > notepad ...) Using Process.Start I can already open it. I would need to find associated applications to extensions in Windows. It can be found in c # ?
You just start the file path and it will open in associated application automatically.
 
Hello
I need to open the files in the application that is associated with the extension. (.docx > Word, .txt > notepad ...) Using Process.Start I can already open it. I would need to find associated applications to extensions in Windows. It can be found in c # ?
As suggested, it happens automatically, just like double-clicking the file in File Explorer. You only need to specify the application you want to use if it's not the default, like right-clicking and selecting Open With in File Explorer. This is the whole point of the file association in the first place.
 
Back
Top Bottom