I have a problem in my project in C# visual studio 2008, I added a textfile in my project,on button click it opens,After deployement (installation)it works fine in window xp but give error in vista,it do not find the path specified.
Folling is the code:

private void pictureBox9_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(@"F:\\DotNet\\Project\\Web\\Web\\Web\\book\\work.txt");
        }

(work.txt file kept in folder "book" in project)

Recommended Answers

All 2 Replies

Char @ escapes a string so remove extra blackslash.

System.Diagnostics.Process.Start(@"F:\DotNet\Project\Web\Web\Web\book\work.txt");

Also, when you deploy the program, is the work.txt file included as part of the installer?
Does your Vista machine have the same file structure and drive letter?
When working with files, it is always possible that the file has been moved/deleted or the system has a different file structure.

There are many ways to store/access files, here are a few i've found useful over the years:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (File.Exists("Docs\\SubFolder.txt"))
            {
                //This file is in the same Directory as the executable
                //You do not need to specify the folder
            }
            else
            {
                throw new FileNotFoundException();
            }
            if (File.Exists("ExecutableFolder.txt"))
            {
                //This file is in the same Directory as the executable
                //You do not need to specify the folder
            }
            else
            {
                throw new FileNotFoundException();
            }
            if (File.Exists(@"C:\Users\Dan\Documents\SpecificFolder.txt"))
            {
                //For files outside the applications executable directory
                //you need to give the exact address
            }
            else
            {
                throw new FileNotFoundException();
            } 
            if (File.Exists(Path.Combine(GetUserDataPath(), "AppData.txt")))
            {
                //This file is stored in the application data file in the Roaming Users directory
                //Each OS has a palce to store app data for roaming/local/all users per application.
                //Using Environment.SpecialFolder ensures you get the correct location if the user has changed the default settings in Windows
            }
            else
            {
                throw new FileNotFoundException();
            }
            if (File.Exists(Path.Combine(GetAppPath(),"Docs\\SubFolder.txt")))
            {
                //alternaitve means to access the directory that the application is running from
            }
            else
            {
                throw new FileNotFoundException();
            }
        }

        public string GetAppPath()
        {

            //get location of executable
            string path = System.Reflection.Assembly.GetExecutingAssembly().Location;

            //strip off name of executable to get directory
            path = path.Substring(0, path.LastIndexOf(@"\") + 1);

            return path;
        }

        private string GetUserDataPath()
        {

            //retrieve location of roaming users application data
            string dir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            dir = Path.Combine(dir, "FileAccessMethodsApp");

            //if application folder doesnt exist, create it
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);
            return dir;
        }
    }

When using the my GetUserDataPath() you can change the value of the Environment.SpecialFolder enum to access local/roaming/all users etc. Check out the msdn link for all the enum values

commented: Thanx for your attention,it may be helpful for me,i'll try. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.