FileNotFoundException is occur while running Wpf application on 32bit machine - c#

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.

Related

How to deploy properly?

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).

Updater.exe built into app.exe

Currently I have 2 exe files. app.exe and updater.exe. When app.exe finds out that there is new version available it runs updater.exe, which downloads and replaces it.
I'm wondering if it's possible to build updater.exe into app.exe. On app start it should check if there is updater.exe in the directory and if it's not than extract it. Any help appreciated.
You can integrate updater.exe as a resource into app.exe. The following SO post shows how to extract it at run-time:
Embedding an external executable inside a C# program
Note that, if your application is located in the default application directory (C:\Program Files, or, more generally, %ProgramFiles%), you will not have permissions to create a file in the same directory (which is a good thing). Thus, you might need to extract updater.exe into a temporary directory that the current user has write permissions to (such as Path.GetTempPath).
When I'm understanding you correctly you need to pack you updater.exe into you app.exe as ressource. Extract it, when App.exe starts.
But I can tell you, this idea with replace is bad. You should choose a MSI package to deploy your application.
For traversing and listing directory contents, see http://msdn.microsoft.com/en-us/library/system.io.directory.aspx
For accessing your embedded resources, see http://msdn.microsoft.com/en-us/library/xc4235zt.aspx
For writing binary data to disk, see http://msdn.microsoft.com/en-us/library/system.io.binarywriter.aspx
For running an exe from disk, see http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx

Program path under Windows XP(C# program)

My program works fine on Windows 7/ Vista and most Windows XPs I've tested on. I open a file that is 1 directory under the program itself. It's Administration\adm.txt (that's exactly the relative directory that I use) . I am using .NET 2.0 for my project.
I tried to run my program on an old Windows XP(which had .NET 2.0 installed) and I got the strangest error I have ever seen in my life. The program runs fine, until the user opens an open file dialog. Afterwards the program started thinking that it was in the directory where the open file dialog last was. So if the open file dialog was last browsing through C:\My Documents\ and I try to open the Administration\adm.txt file I get an exception that "C:\My Documents\Administration\adm.txt" does not exist. Unfortunately I can't use that computer myself again, but I'd really like to know how this happened. Does anybody have any ideas?
Standard behaviour in Windows is for the file dialogs to change the application's current directory. You can prevent it with the OFN_NOCHANGEDIR flag to the OPENFILENAME structure
that is used to configure the open and save dialogs.
For the story, see the Old New Thing blog:
http://blogs.msdn.com/b/oldnewthing/archive/2010/11/12/10089878.aspx
As well as tinman's reply, I will add that it is not a good idea to use a directory that is relative to the current directory, because other processes (including your own as you have seen) can change the current directory. The following code is a more reliable method to create a file name in a directory below your program's directory:
string exeDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string fileName = System.IO.Path.Combine(exeDirectory, System.IO.Path.Combine("Administration", "adm.txt"));

Working Directory in Visual Studio C# file

What exactly is Working Directory in the properties of Visual Studio C# project.
I have see a project where I right click and go to Properties and then I go to Debug tab, it shows me Working Directory where Author of the code has specified folder in local machine. I want to know what does that working directory means and what kinds of file will it store in that directory.
Thank you in advance.
Every process has a current Working Directory which is where all relative paths will be formed from. If not specified, this directory is the directory in which the active application started.
You can check which directory is set by calling:
System.IO.Directory.GetCurrentDirectory();
As mentioned above in a comment by #0xA3 this setting has no effect to your deployed product, it is is only for debugging.
The working directory of a project (and any Windows program in general) is the default place in which a program is looking up it's files. For example: My program has working directory C:\dir and tries to open test.txt, it would look in C:\dir\test.txt.
So every opened file will be opened relative to the working folder.
I think it will store nothing there, unless you add/write code in your application which explicitly creates files, either explicitly in the application's working directory, or implicitly specifying only a filename without specifying a directory at all.

exePath is invalid exception in ConfigurationManager.OpenExeConfiguration

I have a windows service which is installed to C:\Windows\System32. It has a usual .config file, where some app settings are stored. I have another app that writes some values to this config. When i run this app it throws an exception at this line
var config = ConfigurationManager.OpenExeConfiguration(serviceExePath);
The exception says: An error occurred loading a configuration file: The parameter 'exePath' is invalid. Parameter name: exePath
When I put my windows service to another folder everything is ok! Is it due to some access violation rules or smth like that? Is there any way to use System32 folder for my service and open its config?
OS: Windows 7 x64
It is a poor choice for a file location. That directory belongs to Windows, it isn't suitable for your own apps. For one, you'll need admin privileges to open files in that directory. You don't get that without a manifest to trigger the UAC prompt.
For another, that directory is virtualized on the x64 version of Windows. A 32-bit app trying to access files there will be redirected to c:\windows\syswow64.
I could have been more accurate if you posted the stack trace. But, just don't do it.
Note that despite the name of the parameter being 'exePath', you should be passing in the name of the .config file, so you might need to append '.config' based on what's in your serviceExePath folder.
http://msdn.microsoft.com/en-us/library/ms224437.aspx
exePath
Type: System.String
The path of the configuration file. The configuration file resides in the same directory as the executable file.
As it stands, I get the feeling it's trying to load the actual .exe as a config file, which certainly explains why it considers it to be invalid :)
Give this a try, it has helped me in the past:
http://sandblogaspnet.blogspot.com/2009/06/reading-from-configuration-file-in-net.html

Categories