In WPF how do I specify the path to a file nested in a Directory using XAML?

244

Solution 1

Try slashes rather than backslashes, and use an absolute path by leading with a slash:

Source="/Images/unlock.png"

That generally works for me.

Failing that, take a look at Pack URIs.

Solution 2

  1. Add your image to the Project in VS
  2. Right click on that image unlock.png
  3. Go to Context menu /Properties
  4. Change Build Action to Resource

Thats it :-)

Share:
244
Somelizard
Author by

Somelizard

Updated on December 21, 2020

Comments

  • Somelizard
    Somelizard over 3 years

    I am making a program that starts a few programs when you click on a button. I have run .exe files without a problem but when i try to run a .swf it gives me this error:

    java.io.IOException: Cannot run program "C:\\blah\\blah\\blah\\realm.swf": CreateProcess error=193, %1 is not a valid Win32 application
        at java.lang.ProcessBuilder.start(Unknown Source)
        at java.lang.Runtime.exec(Unknown Source)
        at java.lang.Runtime.exec(Unknown Source)
        at java.lang.Runtime.exec(Unknown Source)
        at newbuttonthing.buttonthing$6.widgetSelected(buttonthing.java:152)
        at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:248)
        at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
        at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4353)
        at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1061)
        at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4172)
        at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3761)
        at newbuttonthing.buttonthing.open(buttonthing.java:48)
        at newbuttonthing.buttonthing.main(buttonthing.java:33)
    Caused by: java.io.IOException: CreateProcess error=193, %1 is not a valid Win32 application
        at java.lang.ProcessImpl.create(Native Method)
        at java.lang.ProcessImpl.<init>(Unknown Source)
        at java.lang.ProcessImpl.start(Unknown Source)
        ... 13 more
    

    I am trying to run this:

    Button Realm = new Button(shlStarter, SWT.NONE);
            Realm.addSelectionListener(new SelectionAdapter() {
                @Override
                public void widgetSelected(SelectionEvent e) {
                    try {
                        Runtime.getRuntime().exec(
                                "C:\\Users\\liam\\Desktop\\rotmg\\realm.swf");
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            });
            Realm.setText("Realm");
            Realm.setBounds(308, 50, 116, 25);
    
    • MadProgrammer
      MadProgrammer over 9 years
      swf is a data file, not an executable file. You need to run the flash player. You could try Desktop.open instead
  • MadProgrammer
    MadProgrammer over 9 years
    Or Desktop.open which may provide a platform indepdent solution
  • Brian Hinchey
    Brian Hinchey over 8 years
    The Pack URI link is not working for me. This is an equivalent: msdn.microsoft.com/en-us/library/vstudio/…