1

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??

Community
  • 1
  • 1
user1985189
  • 669
  • 5
  • 16
  • 25

2 Answers2

2

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.

Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
  • 1
    oh awesome! I had tried doing this before but didn't realize you had to put the full file path of the program. I was just typing in "UltraCompare" and getting an error. But it works when I go "C:\Program Files\IDM Computer Solutions\UltraCompare\uc.exe". Thanks Adrian! :) – user1985189 Jan 25 '13 at 16:30
  • um actually there's a problem. UltraCompare allows you to open two files simultaneously and view them side by side. But using this Process.Start() method opens up two separate instances of UC, one for each file. Do you know if there's a way to open both files in the same window? – user1985189 Jan 25 '13 at 16:46
  • Yes that works! But only if I hardcode the filenames... which kind of defeats the purpose of the program if I have to manually change them with every new file. Is there a way to do this dynamically using the textBoxContents values? I tried putting them in quotes and without but neither way seemed to work :( – user1985189 Jan 25 '13 at 17:07
  • @user1985189 Dude, that's just an example. Assemble the string from your variables – Adriano Carneiro Jan 25 '13 at 17:08
  • Sorry, I'm very grateful for your helpful and prompt suggestions, but not sure what you mean by "Assemble the string from your variables". I'm very new to programming in general, let alone C#.NET! What I tried doing was Process.Start("yourprogram.exe", "textBoxContents1 textBoxContents2"); That didn't open properly, and it won't even compile if I omit the quotes. – user1985189 Jan 25 '13 at 17:21
  • 1
    This is waht you need: `Process.Start("yourprogram.exe", textBoxContents1 + " " + textBoxContents2)`. You were passing the names of the variables inside the string. By concatenating (which is what I just showed you), you are assembling the string with the variable contents – Adriano Carneiro Jan 25 '13 at 17:25
0

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

Rohith Nair
  • 1,080
  • 1
  • 17
  • 33
  • Do you mean are the files located on my client machine? Versus on the server? Sorry I'm a coop student and not too sure what you mean... – user1985189 Jan 25 '13 at 16:33
  • Process.start opens up the program in server. If you are hosting the website and using it on same machine and not on other machines, it wont be a problem – Rohith Nair Jan 25 '13 at 16:36
  • Does this mean that if someone else were to run my program on their machine, that it wouldn't work? Because that would be a problem... – user1985189 Jan 25 '13 at 16:39
  • If it is a hosted say in Machine A and someone browses the website from Machine B, this wont work (as the Process is started in Machine A and Machine B user has no way in seeing what is happening on Machine A. Have a look at:- http://stackoverflow.com/questions/7204742/run-exe-on-client-system-from-server-side-c-sharp-code – Rohith Nair Jan 25 '13 at 16:50
  • Ah ok. Hmmm not sure if I'm hosting it or not. Is there a way to check or is it generally the default that the person creating the .NET project is the host? Sorry if that's a stupid question but I am a huge noob in all this.. – user1985189 Jan 25 '13 at 17:09
  • It depends on what you are trying to achieve with this web application. Then only I can help you sort out – Rohith Nair Jan 25 '13 at 20:16