I just finished this tutorial - Create a Picture Viewer - on the MSDN site, and it all works well according to the tutorial specifications (although I did remove the unnecessary buttons).
At the moment, I can open an image file from within the program, which is fine.
However, I'd like to be able to open an image file from Windows Explorer, and have it open in my Image Viewer. (Using the Open With.. context menu).
I did try and open it via the Open With.. menu, but when the program loaded, the image didn't show up. The program just started up as it normally would.
What code do I need to put in, that allows me to open the program via an image file (if that makes sense)
You can use:
Environment.CommandLine
Which will contain stuff in the format of "..." "...", first being the path of your application, and in your case - second would be the path of the opened-with file.
Then, you can split that to get the second "..." and load the file as you normally do in you application.
You can also check to see whether the arguments of the application contain the path of the opened-with file. I'm not sure about that, but it should be very easy to check: Have an mbox which prints the parameters, then try opening a file with your program and see what shows up. Using the args[0] or args[1] or whatever will probably be easier than splitting Environment.CommandLine...
Related
**Hello, my Visual C# is running but when I enter a person's name in Form1.cs[Design] it won't open the file. My code is below and I'll also attach how I saved my txt and what errors are showing. I been trying to figure this out for hours. My goal is to learn how to save txt in my directory folder and how to call it in the .cs code.
`` ```
Errors
enter image description here
tldr;
Your filename is 'BoysNames.txt.txt'.
From the image it is clear that the exception occured, because there is no file called 'BoysNames.txt' in the current directory. I see what the problem is. You have hidden the extension of files and still the text file is displayed with its extension. (Note that since the 'exe' file doesn't show its extension but 'txt' is showing. It means it has double extensions). So, you need to do this:
boysFile = File.OpenText('D:\\visual c#\\pp22\\bin\\Debug\\BoysNames.txt.txt');
To avoid this type of problems in the future, try these steps.
Go to Control Panel.
Click on File Explorer Options -> View
Uncheck this Option 'Hide extensiobns for known file types'
Well it looks like it will not open the file because it is not where you are saying it is. Look in the error, it tells you where it is looking for the file, is it really there?
I am using following code to open an arbitrary file with an arbitrary application that is installed on the system :
if (File.Exists(_document.DocumentFullPath))
{
Process.Start(_document.DocumentFullPath);
}
else MessageBox.Show(string.Format("Document {0} does not exist!", _document.DocumentFullPath));
When I execute this code I see that the file.Exists returns true, so the file does exist. But then the corresponding application opens, for instance an image viewer or a pdf viewer depending on the file type (jpg or pdf) that I am trying to open, but it shows an error in that application that it cannot find the file. No exceptions are thrown.
This does not happen all the time but only for some files.
If I try to open these files manually by copy pasting the content of DocumentFullPath in the explorer, then the file does open correctly in the applicable application.
Duh I don't understand what is wrong.
Example of a filename that don't work :
C:\Users\stuyckp\Documents\Visual Studio 2010\Projects\WPF\FrakoKlantOpvolgingWPF\FrakoKlantOpvolgingWPF\bin\Debug\ProjectDocumenten\11339_Wigbers\6197_koelkast \big-money.jpg
I am running on windows 10.
Do you have spaces in the file path? File.Exists can handle spaces fine, presumably as can Windows Explorer, but it's possible that Process.Start cannot.
I think this is what you want bud. Use the process start using a ProcessStartInfo object with the file path with spaces passed as a string separately.
Use Process.Start with parameters AND spaces in path
so what I'm trying to do is open a file (well, actually two folders, but I figure I'll start with a single file for now) using a third party comparison tool called UltraCompare. I'm working in a C# website project in Visual Studio 2010 (Express edition). I've seen how to open a file using a different program, here: Open a file with Notepad in C#.
Problem is, this only lets you open it using the default program for that file type. But I want to open it in a specified program. For example, a text file should open in UltraCompare, not notepad. Here's the code which does this:
string textBoxContents1 = TextBox1.Text;
Process.Start(textBoxContents1);
The textbox on the webform accepts a string, in which the user types the file's full path (not the most user-friendly design I know, but I'm not sure how to allow them to browse for a folder using a GUI interface in asp.NET). The file is then passed into the Process.Start() method, which opens it using the default program for that file type.
Is there any way to modify this to make it open using UltraCompare??
You can specify the program you want to open the file in:
Process.Start("yourprogram.exe", textBoxContents1);
Update
To open two files in Ultracompare, you'd probably do something like that:
Process.Start("yourprogram.exe", "file1.txt file2.txt");
Keep in mind that the second parameter of Process.Start method are the arguments passed to the program.
I said this is probably going to work because I assumed to be very likely that Ultracompare expects 2 arguments, but this might not be the case.
Quick question: Are you trying to do this for the client machine? Hope not
And I guess it looks into the PATH variable for finding your exe
In my application the user can select reference to file, for example a image file. I would like to make button with a arrow that opens a list with the programs installed on the system witch can open this file type.
I know that I can get the program names from the registry "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts", but how can I filter the entries out that have no meaning - "DllHost.exe, miaui.exe, etc."
And how can I open the file with program that the user choose?
# Lars Tech If I look in registry "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\" and then "OpenWithList" for the extension ".jpg" I see more entries that if if rigt click on jpg file and choose Open With ( see my first images) and I only want those.
And yes there is program's entries that I properly self have added, but that have no meaning to a jpg file. And Windows can filter them out so will I.
I think you can use this article to validate and find out which of them are applicable and valid in your application
If running on Vista or better, you can use
'SHOpenWithDialog'
http://msdn.microsoft.com/en-us/library/bb762234(v=vs.85).aspx
- it's quite straightforward...
Jens
Is there a way to display the contents from memory directly in a Notepad window?
I'm assuming that I understand your question. If the file already exists on the machine you can execute the following:
System.Diagnostics.Process.Start( "notepad.exe", "[PATH]\[FILE].txt");
If not then save the file locally and then run the above code.
Double-click on the file, making sure the association is set to Notepad.
If you want Notepad to show it without saving it to disk, you can open an instance of Notepad, get the handle for the window, then write the text directly into there. You will need to use Windows User APIs to do this.
The easiest way to accomplish this is to save the file and open it in notepad, however there are at least two other ways.
Open Notepad then copy what you want to the clipboard, then using DDE force Notepad to paste. This is bad, because it potentially overwrites what the user may have been doing in the clipboard.
The second way involves getting a window handle to the notepad Edit control, then doing a WM_SETTEXT to the window. This will not, however, work across privilege boundaries (such as for apps that run as administrator, but notepad runs as a normal user). This also involves getting down to Native level and doing P/Invokes. Not exactly an easy method.
Frankly, it's just easiest to save it to a file and load it.
Why do you need notepad to show some contents (which is in memory)?
If you are using winforms, you could put it in a textbox.
Sorry, if I have not understood your question correctly.
I would like to add to MrEdmundo's answer that the Isolated Storage is the right place to store the temporary txt file for Notepad.