Grab the application folder

Asked

Viewed 2,060 times

1

I have an application with all interfaces and features. In the same project I put a simple code running on console to check if there is already a database created on the machine. If not, he must execute the script that creates this. The code is as follows:

public static class Program
    {
        [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
        static extern bool FreeConsole();

        [STAThread]
        public static int Main(string[] args)
        {

            if (args != null && args.Length > 0)
            {

                Console.ReadLine();
                return 0;
            }
            else
            {
                String connString = @"Data Source=.\sqlexpress;Initial Catalog=master;Integrated Security=True";
                String cmdText = "select * from master.dbo.sysdatabases where name= 'db_teste'";
                Boolean bRet;

                SqlConnection sqlConnection = new SqlConnection(connString);
                SqlCommand sqlCmd = new SqlCommand(cmdText, sqlConnection);
                try
                {
                    sqlConnection.Open();
                    SqlDataReader reader = sqlCmd.ExecuteReader();
                    bRet = reader.HasRows;
                    sqlConnection.Close();
                }
                catch (Exception e)
                {
                    bRet = false;
                    sqlConnection.Close();
                    Console.Write(e.Message);
                }

                if (bRet == true)
                {
                    Console.WriteLine("Atualizando Tabelas...");

                }
                else
                {
                    string sqlConnectionString = "Data Source=(local);Initial Catalog=master;Integrated Security=True";
                    var file = new FileInfo(@"C://script.sql");
                    string script = file.OpenText().ReadToEnd();
                    SqlConnection conn = new SqlConnection(sqlConnectionString);
                    Server server = new Server(new ServerConnection(conn));
                    server.ConnectionContext.ExecuteNonQuery(script);

                }
                FreeConsole();
                var app = new App();
                return app.Run();
            }

        }

This is always the first part to run by the program when it runs. Well, I create installers with Visual Studio Installer - > setup Wizard.

How to access the SQL script?

var file = new FileInfo(@"C://script.sql");

In this passage FileInfo will seek as a reference what is already setate, but if during the installation the user chooses another location to install the application the application will not be able to locate the file .sql. How can I fix this?

In the project setup added the file to the project folder where some DLL’s are located.

SS

Error while executing: SS

  • This file will always be in the folder where the application is running?

  • @mustache good that I want but don’t know how to do it during the process of creating the installer. Maybe with the option to add folders to the project, but finally how can I reference if it is in the folder where the application is running?

  • I answered on top of what you told me, if you need to take an arbitrary directory, you need to modify the installer to tell the application where it is, that’s another answer but it’s not even about the application and it would be another question.

1 answer

1


  • what kind of problems would you have? You could even ask a new question. But what would be?

  • The list is long :) The mix of console with WPF, the way it handles exception, opening and closing files and other minor details less important.

  • I actually did not find another solution to be able to run an sql script during the first execution of the software. There are other simpler way instead of console with wpf?

  • Only the WPF. This is invention, is to go from Rio to SP passing by BH.

  • I had an error making fileinfo. I will post the image.

  • This is another problem and has to do with the project and not with the code. You are mixing versions of .Net. Your project must be a huge mess.

Show 1 more comment

Browser other questions tagged

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