• 0

[C#] IO Problems


Question

I have written a module which takes copies music from one selected location to another....

This works grand on vista but when i tried it on XP i keep getting directoryNotFound exceptions.

I know for a fact that the directory does exist so i cant understand what i'm doing wrong

Any help?

Thanks guys

using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;

namespace FYP
{
	public partial class UploadMusic : I_NavigationItem
	{
		private Main parentForm = null;
		private String musicDirectory = null;
		private String applicationPath = null;

		public UploadMusic(Main parentForm, String country)
		{
			this.InitializeComponent();
			this.parentForm = parentForm;

			this.musicDirectory = @"Games\Scrapbook\" + country + @"\Music\";
			this.applicationPath = Properties.Settings.Default.applicationPath;
		}

		private void browseButton_Click(object sender, RoutedEventArgs e)
		{
			Button sendingButton = (Button)sender;
			int browseButton = int.Parse(sendingButton.Tag.ToString());

			System.Windows.Forms.OpenFileDialog openDialog = new System.Windows.Forms.OpenFileDialog();
			openDialog.Filter = "MP3 File (*.mp3)|*.mp3";
			openDialog.ShowDialog();

			switch (browseButton)
			{
				case 1: filePath1.Text = openDialog.FileName.ToString(); break;
				case 2: filePath2.Text = openDialog.FileName.ToString(); break;
				case 3: filePath3.Text = openDialog.FileName.ToString(); break;
				case 4: filePath4.Text = openDialog.FileName.ToString(); break;
			}
		}

		private void uploadButton_Click(object sender, RoutedEventArgs e)
		{
			TextBox[] filePaths = { this.filePath1, this.filePath2, this.filePath3, this.filePath4 };

			// go through each file path searching for images
			for (int filePathNumber = 0; filePathNumber < filePaths.Length; filePathNumber++)
			{
				// check if file path exists on the computer
				if (File.Exists(filePaths[filePathNumber].Text.ToString()) == true)
				{
					if (File.Exists(musicDirectory + Path.GetFileName(filePaths[filePathNumber].Text.ToString())) == false)
					{
						File.Copy(filePaths[filePathNumber].Text.ToString(), musicDirectory + Path.GetFileName(filePaths[filePathNumber].Text.ToString()));
					}
				}
			}

			parentForm.pop();
			parentForm.pop();

		}

		public void navigationChanged()
		{
			// cleanup or refresh when navigation changed
		}

		public void controlClosed()
		{
			// action performed once control permanently closed
		}

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

Thanks guys... Had to install visual C# on an XP machine to try it out

Turns out it was not getting the correct application path on the Windows XP machine.

Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);

Had to use this instead

Path.GetDirectoryName(Application.ExecutablePath);

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.