创建自定义路径地址(wpf)

时间:2011-03-17 18:29:34

标签: c# wpf

目标:
我想在Visual Studio 2010中创建应用程序的路径地址(字符串)。路径是从单位(c或d单位)到应用程序的名称。

问题:
我无法在运行时创建自定义路径地址。我不想要一个从单位到图片名称的完整路径地址。

- 此路径地址为否 “d:\工作\ Modul3 \ Assignment3 \ Assignment3 \ BIN \调试\ logotyp_vp_final.jpg”

- 是的,请求的路径 “d:\工作\ Modul3 \ Assignment3”

请记住,此应用程序及其应用程序名称和地址可能会不时更改。

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);
        }




    }
}

2 个答案:

答案 0 :(得分:1)

获取应用程序执行位置的路径:

string localPath = new Uri( Assembly.GetExecutingAssembly().CodeBase ).LocalPath;
string currentDirectory = Path.GetDirectoryName( localPath );

修改
听起来您正试图访问项目之外的图像。由于这可能适用于您的沙箱环境,因此更好的做法是将图像作为项目的一部分包含在内,并将其作为嵌入式资源进行访问。

这是一个很好的阅读,可以帮助您入门:Adding and Editing Resources (Visual C#)

将图像添加为嵌入资源的步骤

将文件添加到项目中,通常包括以下内容:

+solution
  +project
    +Resources  <-- this is Visual Studio's default folder name for resources
      +SomeDirectory
        -logotyp_vp_final.jpg

然后:

  1. 转到您项目的属性
  2. 点击左侧的资源标签
  3. 选择顶部导航栏上的images资源。
  4. 选择添加资源&gt;添加现有文件。浏览到刚刚放入项目的文件,然后选择添加它。
  5. 该文件现在将显示在项目属性的“资源”选项卡下。在“资源”选项卡中更改文件的名称以使其更有意义。

    现在该文件是项目的嵌入式资源,您可以通过以下代码访问它:

    var MyFile = Properties.Resources.logotyp_vp_final
    

答案 1 :(得分:0)

这样的事情应该有效:

var fullPath = System.IO.Path.GetFullPath("..\\..\\logotyp_vp_final.jpg");