I am will be using this class to set file associations for my program. How would i fetch the path of which file was opened to launch the application? I searched that class for one, and didn't spot one anywhere, and I have searched google and MSDN for the last few hours with no results. I don't have any sample code as I have absolutely no clue.
Any help is thanked and will be welcome.
The filename for the file that was selected, causing your application to open, will be passed as a command line parameter. If this is a program where you're writing the Main function, the array of strings passed to the function will contain the information you need.
public static void Main(string[] args)
Another option, if you aren't writing Main yourself, is to call
Environment.GetCommandLineArgs
See this page http://msdn.microsoft.com/en-us/library/system.environment.getcommandlineargs.aspx for more information about how to use this function.
Related
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
CONCLUSION AT BOTTOM OF POST
I'm a novice with C# and have only just begun using System.IO, but haven't been able to find information on my issue so wanted to ask the guys here:
I have a program that when run, creates a directory, then writes to a .txt file what the most recent value of a certain variable was, so that if the program is interrupted or the computer loses power, restarting the program will retrieve that stored number from the .txt file.
I've simply done this:
string INSTATR = LastValue.ToString();
System.IO.File.WriteAllText(#"C:\\DotTempFiles\\"+Instrument.Name+"ATR.txt", INSTATR);
My first time running this program, the file was created, and I found that on every cycle the number in the file was being overwritten to the last valid number just as I wanted.
However, once I went to the DotTempFiles directory and deleted the .txt file using Shift+Del, the file has never returned upon running the program as I thought it would. If I delete the entire directory and run the program, the directory is recreated but still not the file.
If I run the program using a different Instrument.Name, that new file is created as expected, but the original one that I once manually deleted is still not showing, even after computer restarts.
I can't find any information leads online, so does anyone have an idea? Thank you!
EDIT (more info): People asked a few questions so I'm adding more information (thank you)
There are no errors being reported. It compiles fine, and when running, there is an output window that usually alerts me to errors like if I reference an object that is null, or try to read from an empty file, etc. No errors like this are occurring.
More background on what the program is. There is a stock trading program called NinjaTrader that has their own API based on C#. They wouldn't support the questions I have because it's outside the normal scope of the script development they intended people to use, and that's ok with me. The program itself is a trading strategy that is run within NinjaTrader, and it exposes default programmer access to these two main methods:
Initialize()
protected override void OnBarUpdate() //this is the main part of the program that gets called every time a change has occurred to one of the bars on the stock chart.
In the variable declaration section I have this:
System.IO.DirectoryInfo di = Directory.CreateDirectory(#"C:\\DotTempFiles");
And in OnStartUp() I have this:
//Set up ReadMe file in the temp directory in case people wonder why it keeps appearing
string README = "This DotTempFiles directory is created by the strategy every time it is run, and temporary text files with the name [instrumentsymbol]ATR.txt are written into it containing the last highest ATRStopValue for the strategy, which if it is stopped and restarted with an open position, it will read from that file to get back the best value instead of recalculating it with possibility of a lower unwanted value.";
System.IO.File.WriteAllText(#"C:\\DotTempFiles\\README.txt", README);
Every time OnBarUpdate() is called, which can be up to a few times per second, it first calculates a double called LastValue which is a number related to the stock price, then it converts it to a string INSTATR, then writes it to a .txt file.
string INSTATR = LastValue.ToString();
System.IO.File.WriteAllText(#"C:\\DotTempFiles\\"+Instrument.Name+"ATR.txt", INSTATR);
And this is every single piece of code I have connected to this issue. As I mentioned above, it created the file the first time, but since I've deleted the file in windows explorer, it isn't able to recreate it. However, deleting the directory and restarting the strategy program does recreate that directory.
OnTermination() is not being used to close or delete the file in any way (just in case people wanted to ask) -- I am not using any other code to interact with the file than the ones already shown. Thanks!
EDIT 2 (Update after reading comments): Thank you guys for your comments and help. What I'll do when I get back to my home is try recreating the minimal version of this with an empty program containing just these lines of code and see what the outcome is, and I'll post updates in either way.
EDIT 3: Thank you Steve, that is a very good idea that I should use from now on for these kinds of things.
EDIT 4 (Conclusion): Well, I found out that I overlooked something simple about my own code logic. I was setting the File.WriteAllText to trigger any time the Double variable increased in value, not on every call of the OnBarUpdate method. Because of this, the file would not be written except once every few hours or so and now I see that everything is working properly. I am sorry that I made all these people read this post since it was based on another issue of my own fault. However, I am very thankful to everyone for their comments that helped me get to this point, and to Steve and Mark Lakata for their tips that I learned some new good things from.
I found out there is nothing wrong with the file creation, it was the fact that my coding logic was just calling for it far less frequently than I assumed it was (once every few hours vs several times per second)
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
My application creates files with a ".mhm" extension.
I'd like for my application to open and process these files when I double-click them. Current behavior is that when I double-click the file, my application opens but does not process the file.
How can I read the args parameter of Main() method in C# 2008?
"Hi
My Application creates files with the .MHM file extension.
I would like my application to be associated with this file extension so that when a MHM file is executed, my application is too, with the file path as the arguments for the main method of my application."
That's what I understood.
Grzenio has the right idea -
public void static Main(string args[])
{
...
}
but to push your application into the shell would take extra research, you'd have to look into integrating your application into the Shell (using the registry).
See here: script-to-associate-an-extension-to-a-program
EDIT: Thanks for the edit of the question Chris, trying to see things a bit wider here... Perhaps the arguments solution was all the OP needed.
If I understand your question correctly you just use:
public void static Main(string args[])
{
...
}
When you double-click on a Word document, Word is automatically run and the document is loaded.
What steps are needed to do the same thing with my C# application?
In other words, assume my application uses ".XYZ" data files. I know how to tell Windows to start my application when a .XYZ data file is double clicked. But how do I find out within my application what data file was chosen so I can load it?
Granted this is a VB.NET solution, but this article has details on how to create the file association for your application in the registry and how to retrieve the command arguments when your application is fired up to do the proper file handling.
It looks easy enough to port over to C#.
The arguments should contain the path to the data file.
You can tweak this behavior and pass additional arguments. Look at the example in this image. Here the file path is passed with %1.
Quicktime File Association Dialog
I think what you are looking for is command-line arguments. If you look at the Open action for .doc for example you'll probably see something like this 'word.exe %1'. Windows will take the name of the file and substitute it in for %1 and then execute the command 'word.exe whatever.doc'. Then from within the application you can see what was passed as an argument into the program see this MSDN article for more details.
I believe it is just a command-line argument that gets passed into your app. Then you can read it with Environment.GetCommandLineArgs. I know that is the case if you drag-and-drop a file onto your app. I have not done what you are describing myself but I assume it works the same way.
I did this in a project I was working on awhile ago and don't have the source code handy, but I believe it really came down to this:
//program.cs
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (args.Length > 0)
{
//launch same form as normal or different
Application.Run(new Form1(args));
}
else
{
Application.Run(new Form1());
}
}
args is empty when the application is normally started, but when you've properly linked in the association to your .xyz file, when one of those files is selected, your application will be launched with the file location as the first element of the string[]. Certainly either in program.cs or your launch form, I would add validation, but at the base level I believe this is what you need to do.