I have a console application written in C#, and i want to write the output of my execution in a log file.
In my code actually i use this path : Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
It's working fine when i run with debug command in Visual Studio and my logfile is created and works well.
But when i publish my application, the path is not the local path where the .exe is executed but in my AppData folder ...
I tried many other syntax and all of them works in debug mode but not when i deploy my application.
I want to get the local path, where my .exe is executed to write a logfile here. Thanks.
I deploy it with Visual Studio, simply publish option.
I get a setup.exe and a .application file
Regards,
You in general can never hope to write a file in the same directory as your installed exe. Works when you debug it but not after you deploy it. Programs do not have write access to the C:\Program Files directories. How it ended up in AppData is a bit hard to guess, you would have to intentionally remove the manifest from the program so the OS treats you like a legacy XP program that is not UAC aware. Hmm.
Well, never go there, just use Environment.GetFolderPath() to obtain an AppData path, have your installer create that directory.
You can try getting the Entry Assembly instead (which in your case should be the executable you ran).
var entryAssembly = Assembly.GetEntryAssembly();
var file = Path.Combine(
Path.GetDirectoryName(entryAssembly.Location),
entryAssembly.GetName().Name + ".log"),
Related
I created setup file using Inno setup.I have exe,dll and one xml file in my setup.
When I install on 64 bit machine it works fine means it take xml file from directory where exe is present.
But When I install same setup on 32 bit machine it take dll path but while accessing xml file it takes path of desktop where shortcut of exe is present and showing FileNotFoundException.
Thanks
Your application is most likely not specifying pathnames on the files it is trying to open, so it is expecting to find them in the current directory. Inno Setup by default does not set the "Start In" field on shortcuts its creates; this causes Windows to pick a directory itself, which usually won't be the directory containing your application.
In virtually all cases, this is something that should be corrected at the application level. Properly designed GUI applications should not expect to be started from a particular directory; they should always specify full pathnames on files they open. In Delphi or C++Builder, for example, it's possible to get the full pathname of the directory containing the application EXE by calling: ExtractFilePath(ParamStr(0)). To get the full path of a file named "File.txt" in the application directory, use: ExtractFilePath(ParamStr(0)) + 'File.txt'.
To get path of working directory of application while loading xml file in code.
string WorkingDir=System.AppDomain.CurrentDomain.BaseDirectory
XDocument temp_xdocument= XDocument.Load(WorkingDir+"file.xml");
It works for me.
I have a simple console application written in C#. I want to use its .exe file to run it from SqlServer Agent jon(CmdExec).
I build the application to get the .exe file and now I'm confused about the two folders bin and obj. Both of the folders contain Debug and Release folders and inside there is exe.
Can you advice me, which one is the .exe file I should use for production?
Thanks
You'll want to use the one in the bin folder. Give this a read:
What are the obj and bin folders (created by Visual Studio) used for?
You should use the one in the bin folder.
The obj folder is used to store intermediate files during compilation.
I am trying to make a software that will execute .exe installation files of some other software using c# vs2013. I have used
Process.Start()
I added the files to be extracted in my resources and then I gave the resources path of those files
Process.Start("C:\\Users\\Farjad\\Documents\\Visual Studio 2013\\Projects\\RoyalComputerProject\\RoyalComputerProject\\Resources\\wrar501.exe");
It works fine on my computer However if I try to run it on some other computer it gives me an error saying that specified files are missing. I think It is because of the path I am passing to Process.Start() is of my computer. How should I correct this? What path should I pass? or How should I deploy it?
Ship your app along with dlls and this exe file in a package so when you extract it on another computer, you will get something like
C:/.../Downloads/YourAppName/
- MyApp.exe
- SomeLibrary.dll
- Config.xml
- MyOtherApp.exe
And now, you need to get a correct path of the running assembly which you could do using
string path;
path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
I found this code on MSDN.
For testing purposes, put that exe in bin/Debug or bin/Release folder, depending on how you build your app (debug vs release mode).
I am attempting to get a .Net C# application to run from a Registry Run key (at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run, where other string values live and launch things just fine). But for some inexplicable reason, the path to my app doesn't launch my app. So I decided to run the command manually from a command prompt, just to see if that was the problem. It was. So now I'm really perplexed:
This is a .Net 4.0 C# application which I've compiled in Release mode. The application lives at:
C:\Program Files (x86)\MyCompany\MyProduct\MyProduct.exe
I can double-click on the application and it runs properly. I can also open a CMD window and do the following:
cd "C:\Program Files (x86)\MyCompany\MyProduct\"
MyProduct.exe
And the application launches just fine. HOWEVER, if I try this:
"C:\Program Files (x86)\MyCompany\MyProduct\MyProduct.exe"
The application does not launch. (!) SO obviously the Registry key isn't going to work either.
Is there some kind of additional step which must be taken to run a .Net application from its full path?
Apparently your app depends on the "Current Directory". For opening some sort of file.
The best thing to do is to find that dependency and fix it with an absolute path.
When that's not possible the second-best option would be to change the Current folder to that of the running .EXE as soon as possible. That mean you should execute this line as soon as possible:
Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;
But that will only work if you manage to execute that before the faulty code. Could be hard when it's for instance in a static constructor.
Sounds like you've got a relative file reference to something in the working directory. Is there a file that you're trying to load in your app? Make sure you're using the right file paths.
I followed this link http://www.codeproject.com/Articles/19560/Launching-Your-Application-After-Install-using-Vis
& it has given a good guid to my problem. But in my scenario, I also have a folder with the exe. after installation my exe starts running. but it is not properly linked with the conetent in my folder which is also in same location. How can I link that folder to my exe.
The problem is I added both folder & its contents to Application folder directory given by the setup project wizard. But I can add only the exe to the commit folder as I want my exe to run after after clicking on first initial exe. How ever after first installation both my exe & folder creates & when I click on exe manually it works. But in installation it only copies exe & folder & start running copied exe but could nt find the folder properly.
The working directory of your exe will be different when launched as a commit action.
You either need to change the working directory in your exe or build an absolute path to the folder you are trying to find.
You could pass the application path from the installer to your exe by setting CustomActionData to '[TARGETDIR]\', or extract it from the exe path at runtime, e.g.:
string exepath = Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);
string abspath = Path.Combine(exepath, "yoursubfolder");
I think this is what you want
http://blogs.msdn.com/b/astebner/archive/2006/08/12/696833.aspx