Program open by double click [duplicate] - c#

This question already has answers here:
How to associate a file extension to the current executable in C#
(9 answers)
Closed 9 years ago.
I know there are thousands of this type of question on S.O but i haven't found an appropraite solution that suits my problem.
I have a simple text file create creator that saves file using the default extension .tj. I did this using
DialogResult DR = openFile.ShowDialog();
if (DR == DialogResult.OK)
{
StreamWriter writeFile = new StreamWriter(new FileStream(openFile.FileName,FileMode.CreateNew,FileAccess.Write));
writeFile.WriteLine(rtbText.Text);
writeFile.Flush();
writeFile.Close();
}
This works perfectly but what i want is that whenever my Application is installed and a file with the default extension is saved, Whenever the file is Double Clicked, the file opens in my Application and the Rich Text Box shows the text in the file.
Any help will be appreciated

If you're using click once deployment, there is a way. You can specify file type associations in your installer and these will be put on the target machine for you. Once they launch a file with that association, your app will start. Taken from this MSDN article. If you're using Nullsoft's NSIS installers, you can do something similar, taken from this file type association article
From here, take the answer from this question to grab the string of the file. Then use a StreamReader or similar to read the file's contents and put them in to the rich text box. Voila!
Hope this helps and let me know if you need any clarification.

Related

How to refresh text from a text file [duplicate]

This question already has answers here:
How to detect real-time change of text files?
(3 answers)
Closed 1 year ago.
I am trying to make a chat program which reads text from a text file on a shared folder.
The problem I am having is when you type stuff it doesn't refresh meaning if someone else adds anything else to the text file you wont see it does anyone have an idea of how to fix this.
(this is a console program and I am new to c#)
You will need to either constantly be reading from the text file and have some way to detect changes. Otherwise I would seriously suggest not to use a text file for this kind of application.
Ideally you would want to be able to directly make a push to the application when new data is added. It might be worth researching into Socket Programming.
As #TomBowen suggested, text files should not be used for this purpose. However, for learning purposes, we can oversee that. 😉
I would suggest caching the path to your shared file. Have a loop (preferably async) that would constantly check when the file is updated and save the modified date locally. Do this by checking the last time the file was modified (How do I get modified date from file in C# on Windows Mobile?). If the modification date does not match your saved one - clear the console, read all the text file's content and print it to the console.
This would be a very primitive approach with tons of possibilities for stuff to fail. But it would work just for the sake of making something.

C# Winform: Setting the file save location [duplicate]

This question already has answers here:
What is the best way to store user settings for a .NET application?
(8 answers)
Closed 5 years ago.
My program must produce files from some given data. I'm dealing with PDFs and Excel documents. How do I allow the user to set the directory where the files will be saved? I'm not referring to SaveFileDialog where the user must choose the directory every time.
I want the files to automatically be saved to the directory previously specified by the user. Something to this effect:
Most immediate solution I can think of is to store the directory in a file and read it every time a file is to be saved.
I also read about Properties.Settings.Default.Save(), but is it relevant to my case?
Use FolderBrowserDialog to get the folder...
https://msdn.microsoft.com/en-us/library/system.windows.forms.folderbrowserdialog(v=vs.110).aspx
Get the folder's path.
folderName = folderBrowserDialog1.SelectedPath;
Then go into your project properties (Project menu > Project Name Properties), and click the settings tab. Add a new setting with a name of your choice, like SaveLocation with the type of string. Then you can save it like so...
Settings.Default["SaveLocation"] = folderName;
Properties.Settings.Default.Save();
And then, obviously, retrieve it like so...
string saveLocation = Settings.Default["SaveLocation"]
Read more about saving application settings here: https://msdn.microsoft.com/en-us/library/a65txexh.aspx
You may care to use the registry to store information between sessions. This will require that you have admin privileges. Since this is winform it may be.

Launching Windows Forms via shortcut (.lnk) causes undesirable result [duplicate]

This question already has answers here:
get path for my .exe [duplicate]
(4 answers)
Closed 5 years ago.
I am using a .txt file to save data in a Windows Forms Application. The .txt is located at the same folder as the .exe. However, if I launch the app via shortcut (let's say a desktop shortcut), the app will save the .txt file in the desktop (even though the actual .exe is located somewhere else). The code I use is:
var myFile = File.Create(#"data.txt");
using (var sw = new StreamWriter(#"data.txt", true))
{
sw.WriteLine("I like apples.");
}
If you right click the shortcut and click the Properties link - You will see an option to change the Start In: path - this is where the programs CWD will be...

Add button and custom functionality to adobe reader

I am working on a WPF 4.5 application which needs to interact with PDFs and I am stuck with an issue as described below:
I have template pdfs stored at a specific location. Based on requirement, a copy of the template pdf is created. This pdf has certain fields including text boxes, dropdowns etc. Some of these fields need to be pre-populated like the dropdown values.
Once it is ready, I need to open it, and let the user complete the form. Once completed, the user saves the file and closes it.
Now I need to read the file and send the updated data to the DB. I was able to do all this using iTextSharp by launching the PDF in a separate process and handling the Exited event. Now, the problem I face is this solution does not work if the user uses the SaveAs option to change the name or location of the opened file.
I thought if it would be possible to disable the Save options and add a button on the form clicking which would automatically save the form and close it at the expected location would be a possible solution.
My questions are:
1) Is it possible to find out using the argument of the Exited event handler to find out the saved file name and location? As soon as the user saves the file with a different name, the title of the reader gets updated with the current file name. So I am assuming that the current process is using the latest file.
2) Is it possible to disable the SaveAs and Save file options and close the file on click of a button in the form, using Adobe SDK (JavaScript or plugin or API)?
3) If I use the Adobe SDK, do all the systems on which the application would be installed need to have a licensed version of the Adobe Acrobat?
If the above options are not possible then we would have to settle with dynamic forms. We wanted to experiment with PDF since it is easy to create, and supports image annotations, for which we might need to develop a separate solution, if the above options are not feasible.
I know this is not a very specific programming question, but I need help in order to be able to figure out which path I can go on to be able to achieve the goal.
Please mark duplicate with the link to the other SO question if it a duplicate since I have not been able to figure out one.
Would appreciate answers, links to other posts on SO that are specific to the questions asked.
Please avoid opinion based answers.
Any help would be appreciated.
Any constructive criticism is also welcome.
There is a heavy-handed way to prevent an Acrobat user from Saving a file. In Acrobat, create a Javascript that executes when "Document Will Save." A script like this causes the application to hang rather than Save the file:
var key = "" + this.getField("Password").value;
if (key != "QWERTY") {
app.alert ("No changes to this PDF are allowed,
so you may not Save it.
You will now have to Force Quit or End Task.");
while (true) {};
}
I am not proud of this, but it does the job. You might want to erase the password field before saving.

How to open a file using a specified third party program in C#?

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

Categories