How to open a text document from Explorer? - c#

I'm making a program which saves text documents and also opens them in the program in a textbox, the program works fine and I've managed to make it so I can save the files, and open them when giving the file name
However, I want to know if there's a way to open up explorer, then click the file and have it open in the program, I know Process.Start("explorer.exe"); is used to open explorer, but the files i open from there open in their default programs
If you know how to make it so text documents open in my program please let me know.

I think you need an OpenFileDialog control. which is basically a mini explorer that saves the file name you select in a property. Something like this should work:
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
DialogResult result = ofd.ShowDialog();
if(result != DialogResult.Cancel)
{
richTextBox1.Text = File.ReadAllText(ofd.FileName);
}
}

There's not really an easy way to do what you want (windows explorer integration and change the default 'open with' program). Even if it was easy, it'd be considered bad practice.
If you need your program open a file, the best option is to allow an user to open the file from the program. There's a control called OpenFileDialog in the tool box (assuming you're using WinForms that can do this). Work through this tutorial. It's a bit old, but should get you started.

Related

Opening application with file C# Winforms

So I'm making a Notepad replica in my spare time just for fun, the core of the project is all done. However, I can't open my application from a file. For example, if you had a .txt file, you double click it and it would open the app (by default its Notepad).
I already know how to set the default application, but the code does not support opening the files yet, it can only open files from the menu inside the program.
How would I go about making it so that my application can be opened by files?
in you void Main(string[] args) method, the args will contain the path to the file that was opened with your application. You can also get them anywhere in the application by calling string[] Environment.GetCommandLineArgs(). Print the argument in a message box to see what arguments have been passed.
Here's some code you might want to use for that:
//In a form
public void MyButton_Clicked(object sender, EventArgs e)
{
string[] args = Environment.GetCommandLineArgs();
MessageBox.Show(string.Join(Environment.NewLine, args));
}
I like your question. Once you provide support for opening type files, and you find that your application is still not working, perhaps this might be useful: you can check under registry section HKEY_CLASSES_ROOT for the extension and action details. It talks about default applications, but it gets into more details.
Here's the page:
Finding the default application for opening a particular file type on Windows

C#: Open a Fullscreen process

I'm creating a program that opens to applications (.exe). I can open one of them, that's a windowed application (samp.exe), but I can't open the other one (gta_sa.exe), that's a fullscreen application.
I don't know if using Process.Start() doesn't allow me to open it, but I guess it's not because of it.
Here's my code:
private void btnSA_Click(object sender, EventArgs e)
{
Process.Start(Properties.Settings.Default.SAPath);
}
The Properties.Settings.Default.SAPath is the file path (C:\Program Files (x86)\Steam\steamapps\common\Grand Theft Auto San Andreas\gta_sa.exe)
I found why it wasn't working.
For some reason my computer didn't show up the game because of some configs. After reversing them I managed to open the game normally.
Thank you all for your answers!

opening file directory by not using a string

Ok I may have the title wrong. But this is what I want to do. I know how to open file directories. What I was wander can I use
System.Diagnostic.Process.Start(#" ");
Now the empty quotes is my question. Instead of me manually imputing each application in code. Can is it possible that I can leave it blank.
I am using speech.synthesis and I was wanting to try to avoid if and else statements. However, if I add a new app to my desktop, I would have to update the program for the new app to be apart of the system.
My original way is like this
if (speech == "open notepad")
{
system.Diagnostic.Process.Start(#"notepad.exe);
}
else
{
if (speech == "open wordpad")
{
Sytem.Diagnostic.Process.Start(#"wordpad.exe);
}
So on and so on.. Is there a way to have an open " " that will automatically open the directory when called. So I do not have to keep doing program updates and releases..
This is done in winforms, and is for my own personal use.. The reason why I ask is, if I do release it to the public, then the programs I have listed, the user may or may not have my programs.
if you have the speech-to-text dictionary, why not store the user input into a string, then attempt to open a program with that string. Obviously you'll need to verify it's an installed program. I wish I could give you demo code, but I have no idea what StoT you're using.

How to retrieve the openfiledialog opening path? C#

I have a openfiledialog in my application. I do not set the initial directory and user can navigate to open file and import it to the application (for example imports excel file from C:\ Test location). Next time, when user wants to open another file, system (by default, since initial directory is not set) remember the previous location and openfiledialog opens in that directory (C:\ Test). Is there anyway to retrieve the path that openfiledialog opens? I am sorry if I couldn't explain my problem well and thanks for the help.
Registry is hard to handle, I would suggest that you use Application Properties instead.

OpenFileDialog/c# slow on any file. better solution?

I am opening a file using the OpenFileDialog in c# and I am noticing it is taking between 20-40 seconds to load my file and clear the dialog.
Here is my sample code:
private void btnOpen_Click(object sender, EventArgs e)
{
if (ofdSettings.ShowDialog() == DialogResult.OK)
{
// do nothing
}
}
even with this limited example it takes the 20-40 second duration for the dialog to clear.
the file i'm selecting is a xml file that is only 1.36kb large
I had the same problem, openFileDialog1.ShowDialog() was slow, taking 10 seconds after closing it to execute the next line of my program.
I noticed in the dialog that I had a couple old shortcuts under "Computer" pointing to webdav url's which were no longer valid. I deleted these shortcuts from windows explorer, and the program is fast now.
Check if you have any network connection shortcuts tied to your computer, which also display in the dialog (on the left-hand panel in Windows 7). Try removing them and see if the dialog is faster.
Another option which helped in my case:
OpenFileDialog ofd = new OpenFileDialog
{
...
AutoUpgradeEnabled = false
};
With this option, OpenFileDialog renders simpler UI, "pre-Vista" style according to MSDN article.
I also had this problem when I want to open a example.url file with file open dialog. It takes 0-10 seconds. Then I find out that this has something todo with the file type association (*.url) When I changed the association from default web browser to notepad++ the problem was gone. But I this was no solution for me, because when somebody clicked on a example.url, the default browser should open this file. To solve this I added DereferenceLinks = false.
OpenFileDialog ofd = new OpenFileDialog
{
...
DereferenceLinks = false
};
For me this solution works perfect
You can use a free tool like ProcExp (SysInternals.com) to monitor what your application is doing during the lag. Is it scanning the file system? The registry? The network (maybe it is trying to connect to a network share that is slow to respond).
BTW, you can run ProcExp.exe without installing it from http://live.sysinternals.com/!

Categories