Win form EXE not working from specific path in windows server

Gani_tpt 1,506 Reputation points
2022-01-07T16:50:36.307+00:00

I am developing Win C# application using VS 2019 and is converted into EXE application.

EXE Name : EmpMod.EXE

I am planning to scheduling this EXE into SQL server. I am facing below issue in server.

  1. EXE is not running and it is showing error. The error is showing the could not load the file and path not found.
    Because, EXE is build in local system and in server referring the build local path and not referring server path.
    So, every time i am build the application as EXE again @ server, then i am in a position to use it . then only it's referring the server path.
  2. i am trying to put this EXE into SQL Job scheduler.
    Here the problem is, the Execution path always referring the application in Bin\Debug folder only.
    Ex : cmd /C c:\ANAProject\SourceCode\EmpModule\bin\Debug\EmpMod if i use some other folder, this is not working and showing the in-appropriate path error or some other error

If i manually run from the job scheduler and referring path is bin\debug, my exe working.

If i automatically schedule the job then i am facing the below error.

Also, the below error is showing the Jobs "View History"
Executed as user: SERVERXMN\SYSTEM. The filename, directory name, or volume label syntax is incorrect. Process
``Exit Code '1. The step failed.

This kind of problem will be happening only in VS 2019 build application.

The same if i develop the application in VS 2010 or VS 2013, i don't get any issue at all in the server.

How do we solve the above problem...?

  1. How to solve the referring EXE any path instead of referring from bin\debug.
  2. How to use build EXE in server, which has been developed in local system , the same can be use in server without any newly build in
    server.

This is applicable both in Win/Web application and what are all developed in VS 2019.

pls. guide us, how to solve this type of issues.

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,713 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,245 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 26,201 Reputation points
    2022-01-12T01:18:23.78+00:00

    You really should set aside some time to learn the Visual Studio build basics. The best way to do this is to create demo code that illustrates what happens when a console application is built. Use minimal code.

    Create a new test Console application and name it "ConsoleApp1". Add logic that reads and displays the connection string node from the app.config file.

        class Program
        {
            static void Main(string[] args)
            {
                var connection = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
    
                Console.WriteLine(connection);
            }
        }
    

    app.config example.

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
     <connectionStrings>
     <add name="DefaultConnection" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=DemoDB;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" providerName="System.Data.SqlClient"/>
     </connectionStrings>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
        </startup>
    </configuration>
    

    Do not rename the app.config or run aspnet_regiis.exe. Simply build the application.

    Go to to the project's bin folder and open the Debug folder. You should see 3 files.

    • ConsoleApp1.exe
    • ConsoleApp1.exe.config
    • ConsoleApp1.pdb

    The ConsoleApp1.exe.config file is the result of building the app.config file. The ConsoleApp1.exe is the built C# code and the ConsoleApp1.pdb file is the debugger symbol file used by the Visual Studio debugger to display variable values among other things.

    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 55,686 Reputation points
    2022-01-07T17:14:48.22+00:00

    this is expected. when a program does a create process, the default directory is the the directory the process is running under. unless the tool provides launch folder specification you can not control this.

    the typical workaround is to run a bat file that sets the working folder before running the exe.

    0 comments No comments

  2. Erland Sommarskog 101K Reputation points MVP
    2022-01-07T22:55:11.507+00:00

    Is this executable dependent on a DLL? Then you need to make sure that this DLL is in place as well. Or link the executable to include all libraries to make it a single file.

    Before you start trying to run the application from SQL Sevrer Agent, make sure that you can run it standalone on any random machine you find.


  3. AgaveJoe 26,201 Reputation points
    2022-01-10T20:45:18.99+00:00

    I get the idea that you probably do not know how the build process or aspnet_regiis.exe works.

    First, aspnet_regiis.exe encrypts web.config file sections not app.config file sections. To use aspnet_regiis.exe you have to change the app.config file name to web.config, encrypt the sections, change the file name back to app.config, then build. The build process moves the configuration file to the bin directory and changes the configuration file name to the name of the application with .exe.config appended to the end; EmpMod.exe.config. The final configuration file is not named app.config. If you are moving only the app.config configuration file and not the EmpMod.exe.config to the server then the file not be found error message is expected.

    If I recall correctly you must encrypt the file on the server or use the same machine key as the server on your dev machine.


  4. Dan Guzman 9,206 Reputation points
    2022-01-10T21:28:34.387+00:00

    In addition to to other answers, you cannot schedule a WinForm application to run via SQL Server Agent because there is no interactive desktop thread to process the event pump messages. You need to to use a console application instead.