Skip to content
Advertisement

Getting wrong file path when generating file route in C# Console App

Hi im trying to read in a sql script in a C# console app. Sorry if this is super basic but im having issues as the file route that its generating always starts ion the bin folder of the project.

        public static void ApiResources(IConfiguration config, string testUrlExtension)
        {
            try
            {
                var azureDatabaseUrl = String.Format(config["SqlDatabase:BaseUrl"], $"test{testUrlExtension}");

                SqlConnectionStringBuilder connBuilder = new SqlConnectionStringBuilder();
                connBuilder.DataSource = azureDatabaseUrl;
                connBuilder.UserID = config["SqlDatabase:ZupaKeyReleaseUserName"];
                connBuilder.Password = config["SqlDatabase:ZupaKeyReleasePassword"];
                connBuilder.InitialCatalog = "zupaauthentication";

                using (SqlConnection connection = new SqlConnection(connBuilder.ConnectionString))
                {
                    using (SqlCommand command = connection.CreateCommand())
                    {
                        connection.Open();
                        var GetLocalPathToProject = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Split("Zupa.ReleaseDeploymentAutoConfigure")[0];
                        var routeToApiResourseSqlScript = $"{GetLocalPathToProject}Zupa.ReleaseDeploymentAutoConfigure\Zupa.ReleaseDeploymentAutoConfigure\Sql\Scripts\";
                        var apiResourcesFileName = "AddApiResorces.sql";
                        var fullPathToSqlScript = $"{routeToApiResourseSqlScript}{apiResourcesFileName}";

                        command.CommandText = File.ReadAllText(fullPathToSqlScript);
                        command.ExecuteNonQuery();

                        connection.Close();
                    }
                }
            }
            catch (SqlException e)
            {
                Console.WriteLine(e.InnerException);
            }
        }

The error im recieving is as follows:

Something went wrong try configuring the release again. System.IO.IOException: The filename, directory name, or volume label syntax is incorrect. :

‘C:Zupa_Source_CodeZupa.ReleaseDeploymentAutoConfigureZupa.ReleaseDeploymentAutoConfigurebinDebugnetcoreapp3.1file:C:Zupa_Source_CodeZupa.ReleaseDeploymentAutoConfigureZupa.ReleaseDeploymentAutoConfigureSqlScriptsAddApiResorces.sql’

The correct path is being added to the end of the bin directory which is

file:C:Zupa_Source_CodeZupa.ReleaseDeploymentAutoConfigureZupa.ReleaseDeploymentAutoConfigureSqlScriptsAddApiResorces.sql

Thanks in advance, Chris.

Advertisement

Answer

Change “CodeBase” in this line

var GetLocalPathToProject = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase).Split("Zupa.ReleaseDeploymentAutoConfigure")[0];

to be Location:

var GetLocalPathToProject = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location).Split("Zupa.ReleaseDeploymentAutoConfigure")[0];
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement