简体   繁体   中英

C# application not running on different machines

I have a C# application that loads fine on my development machine but fails to even start on Win2008 machine.

Checked Frameworks and they match up, Net 4.0

I immediately suspected the problem was arising from references to specific files that I was reading from and sure enough, using some test code I narrowed it down to a single line.

public static string[] salesArray = (File.ReadAllLines("sales.txt").ToArray());

If I comment out the above line, the test app starts, if I leave it in, it fails. Any ideas?

I am copying the Debug directory to the second machine (sales.txt) within it.

This is the entire code. The app does nothing but open a blank window.

namespace testServer
{
    public partial class Form1 : Form
    {

        public static string[] salesArray = (File.ReadAllLines("sales.txt").ToArray());

        public Form1()
        {
            InitializeComponent();
        }
    }
}

Two potential issues jump out:

1) The current working directory of the app isn't what you think it is: If you can print/show this, you would know for sure. http://msdn.microsoft.com/en-us/library/system.io.directory.getcurrentdirectory.aspx

Because you are specifying a relative path, the current working directory is what the file is being resolved against.

2) Perhaps permissions. You might right-click, 'Run as administrator' as a quick check into that theory.

You should use a better mechanism to find that file, and check for its existence (ie: File.Exists ) prior to opening it.

This will also let you report to the user if there is a problem, such as the file not existing where you expect it to be.

Put an exception handler around the code. You can get the error message and can handle the failure gracefully.

It should handle all errors (file not found, file in use, permission errors, etc.).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM