Program path under Windows XP(C# program) - c#

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"));

Related

FileNotFoundException is occur while running Wpf application on 32bit machine

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.

Application will not launch from command line full path, but will after CDing to directory

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.

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.

Create a Setup File Windows.Net C#

I have created a windows application setup program, it needs to have a text file in the application folder. The file is also included while creating the setup.
Once the setup successfully completes and my program tries to modify the file based on user input, its simple throwing an exception.
I am using Windows 7 Home Premium OS.
Any suggestion/help will be great to overcome this issue.
This is normal on a Vista or Win7 machine. Or a properly secured XP machine for that matter. The normal install location for programs, like c:\program files\your company\your app, is read only for most users. UAC is a counter-measure to malware messing with programs.
You'll need to store the text file into a writable location, the AppData folder. In the Setup project, right-click "File system on target machine" and select User's Application Data Folder. Find that file back at runtime through Environment.GetFolderPath, passing Environment.SpecialFolder.ApplicationData. Or use "User's Personal Data Folder" if the user should be able to find it back easily through the Documents folder.
What exception is being thrown? It could be a UAC issue.

a Problem with Windows Help Button (EDIT-New problem)

a strange behavior happens when install the application and then run the exe file.
first let me explain my steps:
1) i added HelpProvidor and i set HelpNameSpace property to the chm file path.
2) in the form properties i set HelpNavigator on helpProvidor property to : KeywordIndex
and HelpString on HelpProvidor to Use ASGATech Quran Sound Downloder Help
3) i created an installation pkg. and it runs successfuly.
My problem is when i debug the application and presses on "?" (which is the help button) it displays the chm file.
but when i run the sortcut on the desktop which has been created from the installation package it doesn't.
why is that the HelpNamespce is the same. and the exe which is in the Application folder is that same so why is that.
the exe file is the same and also when i created a new item in start -> program menu the exe file that created runs the chm file but the shortcut doesn't.
i put the chm with the application folder which throws the application folder on:
c:\program Files\Manufacturer\ProductName
and it put the file on this path successfuly so why he can't see it.
EDIT:
i found that the problem is when i uninstall the application it keeps the product folder
it only removes the icon ans chm and so when u reinstall the application he dont feel the new state (that he has chm file)
now how could i remove the product file from programFiles.
Are you sure the exe file is the same?
It seems that either the help file is not found by your application or that the current security settings of IE prevent it from being loaded.
Use a tool like Procmon to see where your application is looking for the help file.
If it is not found make sure that it is included within your installer package.
If it is found make sure that it is on the local disc and that the default security settings for your local machine have not been changed within IE.

Categories