Probably an easy one for ya - File Associations

  • Thread starter Thread starter Tim Geiges
  • Start date Start date
T

Tim Geiges

I am trying to open an image in a little image viewer I wrote(it has
its own file explorer but I want it to also open from Windows Explorer)


When I select an image from widows explorer and use "Open With"
point it to my app, I can read the args[0] which contains the path to
the file
but I cannot pass the argument to another method to open the image
window

public string thefile = "";

public static void Main(string[] args)
{
Application.Run(new MainForm());
thefile = args[0].ToString();
}

My Documents\SharpDevelop Projects\imgbrowse\MainForm.cs(62,4): error
CS0120: An object reference is required for the nonstatic field,
method, or property 'imgbrowse.MainForm.thefile'

how can I pass the value of args[0] from Main to my ImageView method?
 
Tim Geiges said:
I am trying to open an image in a little image viewer I wrote(it has
its own file explorer but I want it to also open from Windows Explorer)


When I select an image from widows explorer and use "Open With"
point it to my app, I can read the args[0] which contains the path to
the file
but I cannot pass the argument to another method to open the image
window

public string thefile = "";

public static void Main(string[] args)
{
Application.Run(new MainForm());
thefile = args[0].ToString();
}

My Documents\SharpDevelop Projects\imgbrowse\MainForm.cs(62,4): error
CS0120: An object reference is required for the nonstatic field,
method, or property 'imgbrowse.MainForm.thefile'

how can I pass the value of args[0] from Main to my ImageView method?

Well, for one thing, Application.Run isn't going to return until your
form has been closed, which probably isn't what you want.

The easiest thing to do would be to make the MainForm constructor take
a string[] parameter, and pass the arguments to that:

[STAThread]
public static void Main(string[] args)
{
Application.Run (new MainForm(args));
}

Then in the constructor, deal with the arguments appropriately.
 
OK I see where my problem was, that helped out, Thank you very much
I have one other question, now that I can read the argument properly
my app crashes unless I pass an argument, (I am very new to c# BTW) is
there a way to test args[0] to make sure it exists before trying to set
a string to the value of args[0] hence causing the crash.
if I do

if(args[0] != "") I get an exception
if I set the value of a string to args[0] then do if(mystring != "")
I also get an exception because args[0] does not exist.
 
args.Length > 0

--
Thanks
Wayne Sepega
Jacksonville, Fl


"When a man sits with a pretty girl for an hour, it seems like a minute. But
let him sit on a hot stove for a minute and it's longer than any hour.
That's relativity." - Albert Einstein
 
Sorry got another quick one(I think)
when I right click a file (image) and choose "open with" and I browse
for my app, once I select my app it shows up int the "Open With" Dialog
box under "Reccomended Programs" but only the icon there is no
filename or text.
is there a property I need to populate, I have the Text property filled.
 
Tim Geiges said:
Sorry got another quick one(I think)
when I right click a file (image) and choose "open with" and I browse
for my app, once I select my app it shows up int the "Open With" Dialog
box under "Reccomended Programs" but only the icon there is no
filename or text.
is there a property I need to populate, I have the Text property filled.

I don't know what gets used there, I'm afraid.
 
Back
Top