Visual Studio - Read specified file on launch - c#

I'm working with a simple program that has to read data from a txt file and than display it. The thing is that i would like to read a specified extension files (for example .log) automatically.
So, i would like to do it as a simple Windows user - double click file and than the program opens. The problem is that this program is working but it's nor loading the file on start and tbh i have no idea if I should program it somehow? Any hints?
Thanks!

The easiest way to make it load the file automatically is to allow it accept the file path as a parameter and then do a load operation. As for having your program open automatically when a user double clicks on that file type I would recommend checking out this CodeProject

Sound like you want to associate file type with your application. This is done via windows registers. Try for example this article http://msdn.microsoft.com/en-us/library/windows/desktop/hh127451(v=vs.85).aspx

Related

Creating File Extensions for my application

I'm wondering how I can create my own file extension to work with my application. For example, say I wanted to have a ".abc" extension. The full file name example would be "MyFile.abc".
I want the file to behave in a way that when it is double clicked, it will open up in my application, but actually it contains data of a .xml file.
Sorry if this makes little sense, this is completely new to me. I'm using c# in visual studio 2013, and basically my application is an automatic update installer. I want it so when my file with my own extension is selected, my application opens and uses the xml data from it to do the update.
You can create your own files with custom extensions by saving the file with that particular extension. Then what you need is to associate the extension with your application. For this you need to create the instance of FileAssociationInfo .
Check this out System-File-Association

Back-up my /Data folder in my c# project

as a starter of c# (And honestly not really loving the visual express c# environment) I'm in need of a back-up function to finish off my program.
I have 4 xml files in the /Data folder (In my project's root)
Now, what I want to do is back these files up in a file-save sense.
When a user clicks the "Back-up 1.xml!" button I want the program to open a saveDialog which automatically saves a pre-defined XML file (/Data/1.xml), to wherever the user wants, under whatever name the user wants.
However, I just cannot get googled how to make c# preselect that file. I only get these plugins from MSDN (which isn't very usefull either)
Any help would be appreciated, thanks in advance!
It looks like you need to copy a file from one location to another one.
Here is another StackOverflow thread that looks similarly to yours:
Copy a File
Esentially, the only thing you need to do:
System.IO.File.Copy(oldPathAndName, newPathAndName);
You can get newPathAndName from user input. You know oldPathAndName already.

Open file on start up

I don't know what it is called but I want to be able to double click on my saved file and then my program should open and load the file. What is this called and how do I do it?
I am using c# wpf and .net 4.0
BR
How about the last 2 fields, what am I supposed to write there?
That is a file association, if you want this to happen on a client machine you need to register your application as the default application for a given extension. This question might be handy.
To actually handle the opening you need to process the arguments that are handed to your application, they will contain the file path. You can get the arguments either in the override of Application.OnStartup (e.Args) or Environment.GetCommandLineArgs.
you need to register the file extension and associate it to your program, either during the setup using certain APIs or from code when program executes the first time.
check these ones:
How to associate a file extension to the current executable in C#
Associate File Extension with Application
personally I do not like the 100% registry approach, there should be some Windows APIs for that and we should let those APIs to work without worrying about the Registry from our side, in my opinion.

Integrating my new program with Windows

I've written a log parser, with some generous and insightful help from the SO community:
Keeping the UI responsive while parsing a very large logfile
Now, I'd like to be able to right click one of these logs, select "MyNewLogParser" from "Open With.." and see it open in my new program.
This would require me to
Change something about my XP installation to show my program in the dropdown list
Change the program so that it knows to open the selected file and run the parsing.
What do you call these things, and how is it done? I don't know what to search for...
To open the selected file, you need to implement command-line parameters. Take a look at your Program.cs file and the Main function.
You want its signature to look something like this:
static void Main(string[] args)
{
}
The args array will be the array of command-line params passed to your application. So if you ran MyNewLogParser myLog.txt, the contents of args[0] would be myLog.txt.
For the OpenWith... menu, you'll need to modify the registry. Search for the "OpenWith" key in Regedit and you'll find it. On my machine (Windows 7), it's in HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts. I'm not sure on the exact details of how it works, but Google should be able to help you out from there.
If you don't want to do it programmatically, I'm pretty sure there's some menu item that allows you to select the application that will open a file. Don't recall what it is on XP, though. Alternatively, you can associate a file extension with your application through a tab in the Folder Options dialog, so that double-clicking it opens your application.
Assuming that your file logs have specific file extension, you need to add OpenWithList keys in the registry. See this MSDN page for more information:
http://msdn.microsoft.com/en-us/library/bb166549%28VS.80%29.aspx

How can I make an application open when a user double clicks on its associated file?

I am creating an application that uses a certain file format as its data source. I want this application to open whenever the user double clicks on this file, like how MS Word will open when a user double clicks on a Word document. How do I accomplish this? Also how would I populate the data fields using the file that the user selected. Would I use args[] from the program.cs class? I am using c# to code this application.
N.B. I want this association to be made when the application is installed on the host machine without the user doing anything.
FIRST, you need to set up file association, so that your file type is associated with your application and opening the file type will run your application.
You can do the file association programatically, there is some detail here as mentioned:
http://www.codeproject.com/KB/dotnet/System_File_Association.aspx
You can also do it via your Setup project for you application if you have one. This is an easier path for "newbies". Details for using visual studio to get the setup project to add the file association and also set the icon for the file are here:
http://www.dreamincode.net/forums/topic/58005-file-associations-in-visual-studio/
Otherwise if you use InnoSetup, Wix etc then I suppose you could just see instructions for those installers to create the association for you.
SECOND, you need to have your application accept command line arguments. The opened file(s) is(are) passed as a command line argument(s). You need to process the arguments to get the file path/name(s) and open the given file(s). There is a nice description of this here with code:
C# Command Line arguments problem in Release build
In your case, rather than MessageBox.Show(s) in the form shown handler, you would call your bespoke argument parsing method.
For a simple application which only accepts files names to open as arguments, this could be as simple as
foreach (string filePathName in Args)
DoNamedFileOpen(filePathName);
Your code can also have a method that might extract from the file the values for the datafields you are interested in etc.
This is a nice simple approach to the issue of have file associations set on installation of your application, with icons, and having your application handle the opening of those files.
Of course, there are plenty of other options, like run-time file association (asking the user if they want the association), detecting "broken" associations, etc.
This question is a long time here but I hope this is useful for new searches
See this. Or this if you want API information.
ClickOnce supports file associations as of .NET 3.5 SP1, too. In the project's properties, switch to the Publish tab and click the Options button. There's a File Associations section in that dialog that allows you to specify file extensions, descriptions and custom icons.
First, you have to associate the filetype extention with your executeable. On Windows you do this via the registry (search "filetype association windows"). In this question you find some interesting hints: Filetype association with application (C#) Script to associate an extension to a program
Your program has to react on the command line arguments or parameters. In Java, it is indeed the string array of the main method. I would gess, it's the same in C#.
If you don't need to do it pro programatically, right click on the icon, click open with ..., then select 'always use this program ...'.
This is something usually handled by your setup program .. I've used INNO setup for example, and it's trivially simple to arrange for it to adjust user's registry to launch your app when associated file extension is double clicked/opened. It'll even take care of MIME types for you as well as clearing these things on uninstall, which is a very nice thing to do
I managed to solve this issue. I used WIX to create an install file and asked it to associate the file with the application when it installs.

Categories

Resources