Reputation: 14108
Goal:
I want to achieve the path address, as string, of the application created in Visual studio 2010. The path is from unit (c or d unit) to the name of the application.
Problem:
I can't create a customized path address in runtime. I don't want a full path address that is from unit to the name of the picture.
--No to this path address "D:\work\Modul3\Assignment3\Assignment3\bin\Debug\logotyp_vp_final.jpg"
--Yes, requested path "D:\work\Modul3\Assignment3"
Please remember that this application and its name of the application and the address can be changed from time to time.
namespace Assignment3
{
/// <summary>
/// Interaction logic for FlightForm.xaml
/// </summary>
public partial class FlightForm : Window
{
public delegate void TakeOffHandler(object source, TakeOffEventArgs e);
public delegate void ChangeHandler(object source, ChangeRouteEventArgs e);
public event TakeOffHandler TakeOffEvent;
public event ChangeHandler ChangeEvent;
public FlightForm()
{
InitializeComponent();
Title = "Flight ";
cmbStatus.Visibility = Visibility.Hidden;
btnLand.Visibility = Visibility.Hidden;
string fullPath;
fullPath = System.IO.Path.GetFullPath("logotyp_vp_final.jpg");
BitmapImage image = new BitmapImage();
image.BeginInit();
image.UriSource = new Uri(fullPath);
image.EndInit();
image1.Source = image;
System.Windows.Shapes.Path path = new System.Windows.Shapes.Path();
}
private void btnStart_Click(object sender, RoutedEventArgs e)
{
cmbStatus.Visibility = Visibility.Visible;
btnLand.Visibility = Visibility.Visible;
btnStart.Visibility = Visibility.Hidden;
TakeOffEvent(this, new TakeOffEventArgs("a", "b", DateTime.Now.ToString()));
ChangeEvent(this, new ChangeRouteEventArgs("aa", "bb", "cc"));
}
}
}
namespace Assignment3
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class ControlTower : Window
{
public ControlTower()
{
InitializeComponent();
}
private FlightForm myFlightForm;
private void btnSendNextAirplane_Click(object sender, RoutedEventArgs e)
{
myFlightForm = new FlightForm();
myFlightForm.TakeOffEvent += new FlightForm.TakeOffHandler(PrintOutTakeOff);
myFlightForm.ChangeEvent += new FlightForm.ChangeHandler(PrintOutChange);
myFlightForm.Show();
}
public void PrintOutTakeOff(object source, TakeOffEventArgs e)
{
lstDisplay.Items.Add(new { FlightCode = e.FlightCode, Status = e.Status, Time = e.Time });
}
public void PrintOutChange(object source, ChangeRouteEventArgs e)
{
string test = e.FlightCode + e.Status + e.Time;
MessageBox.Show(test);
}
}
}
Upvotes: 1
Views: 297
Reputation: 38385
To get the path of where the application is executing from:
string localPath = new Uri( Assembly.GetExecutingAssembly().CodeBase ).LocalPath;
string currentDirectory = Path.GetDirectoryName( localPath );
edit
It sounds like you are attempting to access images outside of your project. As this may work in your sandbox environment, a better practice is to include the images as a part of your project and access them as an embedded resource.
Here's a good read to get you started: Adding and Editing Resources (Visual C#)
Walk-through of adding an image as an embedded resource
Add the file to your project, typically something along the lines of:
+solution
+project
+Resources <-- this is Visual Studio's default folder name for resources
+SomeDirectory
-logotyp_vp_final.jpg
Then:
images
resource on the top nav bar. The file will now show up under your Resources tab of your project's properties. Change the name of your file in the Resources tab to be more meaningful.
Now that the file is an embedded resource of your project, you can access it by the following in code:
var MyFile = Properties.Resources.logotyp_vp_final
Upvotes: 1
Reputation: 60051
Something like this should work:
var fullPath = System.IO.Path.GetFullPath("..\\..\\logotyp_vp_final.jpg");
Upvotes: 0