question

GaniTPT avatar image
0 Votes"
GaniTPT asked AgaveJoe edited

Win form EXE not working from specific path in windows server

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-generaldotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AgaveJoe avatar image
0 Votes"
AgaveJoe answered AgaveJoe edited

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ErlandSommarskog avatar image
0 Votes"
ErlandSommarskog answered AgaveJoe converted comment to answer

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.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

We developed C# console application. Interestingly, we encrypted application and we executed exe file in separate folder.

It's working fine in local system.

But, the same steps i used in server. I am getting below error.

I am using only two files( EXE and config file with encrypted) ==> It's working in local, But, not in server.

pls. refer the below screenshot.


163643-image.png


163560-image.png


0 Votes 0 ·
image.png (29.9 KiB)
image.png (4.9 KiB)

First i encrypted the app.config file using the below comments

aspnet_regiis.exe -pef "connectionStrings" "C:\AMSProject\Source\Dev\API_EncryptCode"

The app.config has encrypted successfully. when i check bin\debug\API.exe configuration still not encrypted. When i click the exe here it's working fine.

So i decided again,i build the application. now, bin\debug\API.exe configuration file has encrypted. Now, when i am clicking the exe file, the exe shows the mentioned error.

what we want to do here..?


0 Votes 0 ·
AgaveJoe avatar image
0 Votes"
AgaveJoe answered GaniTPT commented

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.




· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I did the below steps as you mentioned.

  1. My original source code kept it in server.

  2. Changed App.config to web.config

  3. Encrypted using aspnet_regiis. Now App.config encrypted

  4. I Changed back to App.config

  5. I build the application again.

  6. Now, bin\debug\API.exe also encrypted. The filename will be in the folder bin\debug\ "API.exe" not API.exe.config

  7. I am trying to click the EXE file again from bin\debug\API ==> API is the main application EXE file.

  8. Here again i am getting the same error.

After encrypting and then build the we are facing the issue. we are not even moved anywhere the EXE file.
Just executing the file from bin\debug folder (After encrypting and then build)

I am really frustrated because of this issue and why it will come like this...?



0 Votes 0 ·
DanGuzman avatar image
0 Votes"
DanGuzman answered ErlandSommarskog commented

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.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks. I already converted console application.

0 Votes 0 ·

Maybe you did in a way, but not fully. In the screenshot above there is a desktop interaction, through Dr Watson. A true console application should not do that. And particularly not an application that you start from SQL Server Agent as Dan points out.

So you need to make sure that you don't have any assemblies that can interact with the desktop. I don't really understand the deliberations on the app.config etc. When I write console-mode programs, I typically have the connection string hard-coded into the program. But I should hasten to add that my .NET competence is restricted to writing very simple data-access programs for testing purposes - I'm an SQL Server guy.

It's not uncommon that my small program crashes - but they never result in a Watson window, but it's only a stack dump in the command-line window.

0 Votes 0 ·