Pregunta

I have added a text file into Windows form Application and trying to access this file.

Here is my code..

static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        System.String s = Application.StartupPath+"/Licence.txt";
        System.IO.FileInfo fi = new System.IO.FileInfo(s);

        if (fi.Exists)
        {
            if (fi.Length == 0)
            {
                Application.Run(new Form1());
            }
        }
    }

Licence.txt is there in my project folder but every time (fi.Exists) comes to be false only and program comes out of the flow without showing Form1() window.. Please Help me. Thanks in advance..

¿Fue útil?

Solución

Use this:

string s = Path.Combine(
  Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
  "Licence.txt");

Do not try to combine paths manually because you must deal with path delimiters... using builtin functions is much better!

Finally remember to set Copy to Output property on License.txt file inside your project.

Otros consejos

Without doing a string concatenation use Path.Combine()

System.String s = Path.Combine(Application.StartupPath,"Licence.txt");

[1] The question is where it is in your project folder :) Application.StartupPath gets in debug mode

yourProject\bin\Debug

is your file there?

try step-by-step debugging and look which path you get and if the file is there

[2] Your Window is not shown, because you missed adding

Application.Run(new yourForm());

Hope it helps :)

string filePath = Path.GetFullPath("Licence.txt");

This file should present inside installation folder. So that you can get filepath.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top