Run a form (or create file) only when the program is first opened

Asked

Viewed 126 times

1

I want to know if there is any way I can create a form to be opened only the first time the program runs. Because the form must create a file in the system with login the first time the program runs only, it cannot create the file whenever the user opens the program because if the login has been changed it will be changed again to the default.

Another option would be to create the file at the time of installing the program but do not know how to do it, I already researched enough and found nothing.

There’s no code because I couldn’t do anything, I just tried to put File.Create("caminho"); on splash screen, but it always creates a new file and replaces the old one thus losing the information recorded in it.

  • Tried to create a boolean global variable?

  • 1

    do a check! if with File.Create("path") work, do something like that FileStream f = File.Open("path") and you give the condition If (f.exist) "exacuta A" else executa B" create a function with FileStream() to check if the file exists.

  • Had not tried with global variable, I decided to try Helbert’s method first, because variable were resetting its value when the program was restarted (but I did not use global) .. Well the Helbert method worked, I just had to research, actually it’s not Filestream or File.Open.. I’ll leave the answer below..

  • Now there is the question of how to create forms to be executed only the first time, but with file you can control better.. I think it would be something like this, in the first run, it shows the form and after the user click on the continue button for example, delete the form forever or change the program startup form

  • my fault! is FileInfo I used it once for a similar problem, and I tried to respond in my head, but that’s it and... it is possible to control this if your application has database, or you can use the setings of the project.

1 answer

1


Well, I got it here, it was like this, right under the InitializeComponent(); from the splash screen (for it to create before opening the first form)

//Cria variavel com uma nova instância com informações de tal arquivo
FileInfo arq = new FileInfo("caminho-arquivo");

// se o arquivo não existir (por isso == false) ele é criado
if (arq.Exists == false)
{
    File.Create("caminho-arquivo");
}
// se não ele não faz nada, continua sua execução normal
else{}

Remembering that you should import the IO library (using System.IO;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.