|
|
// How do I make the current directory the
//same as where the program resides.
static void Main(string[] args)
{
int mode=0;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (args.Length == 0)
{
MessageBox.Show(Environment.CurrentDirectory);
// Current directory is where the program resides
// which is what I want
Application.Run(new Form1(mode));
return;
}
if (args.Length == 1)
{
MessageBox.Show(Environment.CurrentDirectory);
// Current Directory becomes \Documents and Settings....
// or \Program Files\Common Files\System\Mapi\NT and I want
// the CurrentDirectory to be the location of the program
mode = 1;
Application.Run(new Form1(mode));
return;
}
}
|
|
|
|
|
I think what you are looking for is:
Application.ExecutablePath
|
|
|
|
|
TNX
I was able to solve my problem although I still don't understand why the directory changes when there is a command line arg[0].
|
|
|
|
|
The current directory isn't set based on the location of the EXE. For command-prompts, it's based on the current directory. So if you have a command-prompt open to say "C:\Temp", then run your application like "C:\Blah\myApp.exe", then the current directory is "C:\Temp". For Windows short-cuts, you can specify the "Start in" directory, which is the same thing.
You can get the path to the entry assembly (the EXE in the general case) like so:
System.Reflection.Assembly assembly = System.Reflection.Assembly.GetEntryAssembly();
Environment.CurrentDirectory = System.IO.Path.GetDirectoryName(assembly.Location);
Or check out the other response, which talks about Application.ExecutablePath
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
TNX Tom
Application.Executable Path solved my problem is there any disadvantage to doing things this way?
|
|
|
|
|
Not at all. If you use Reflector[^] you can view the code for this property. Internally, it uses GetEntryAssembly(), but also handles other cases. So Application.ExecutablePath would probably be the best way to go.
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
This is why you never write code that uses the "current" directory. You ALWAYS write your code to generate fully qualified path names when working with files.
|
|
|
|
|
Hy
Can anybody tell me what is the difference between KeyPress event and OnKeyPress override? Why we need both? Why don't work in the same time, only one run?
thx
|
|
|
|
|
The KeyPress event can be used by the associated class or other classes. For example, a Form can listen for key pressed on a TextBox that it contains. The OnKeyPress method is used to fire the KeyPress event. This can only be used by dervied types. So if you create a custom TextBox (say MyTextBox), then you can override the OnKeyPress to catch key presses, alter when the KeyPress event is fired, fire another event, etc.
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
Hello,
Way 1)
OnKeyPress ist the virtual method of a class, which can be overriden from an inherited class.
Way 2)
For handling the KeyPress event, you do not have to inherit a class.
You instanciate a class and handle the delegate.
All the best,
Martin
|
|
|
|
|
Hi,
I am currently writing an application that will dynamically load different applications and contain them in its own form (its an MdiManager Form). A big issue that I am running into is as follows:
When my main application loads and constructs another program, the child program is blowing up because the ConfigurationManager is using the main containing application's app.config. What I can't find....How in a dynamic environment do I get the current application's resulting exe name.
All of these classes are returning the name of the containing application:
Application.ProductName
Assembly.GetEntryAssembly().GetName().ToString()
I am trying to avoid hard coding "MyClass.exe.config" into each child application. I would much rather find a new method that I could use from now on instead of relying just on the ConfigurationManager. Thanks
|
|
|
|
|
You could get the path to the child application like so (assuming that the Program class below is in your child application):
static class Program {
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
public static String GetPath() {
Assembly assembly = Assembly.GetExecutingAssembly();
return System.IO.Path.GetDirectoryName(assembly.Location);
}
}
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
Sorry, you can get the EXE name like so:
static class Program {
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
public static String GetExeName() {
Assembly assembly = Assembly.GetExecutingAssembly();
return System.IO.Path.GetFileName(assembly.Location);
}
}
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
Hi friends,
My Requriment is When The Keyboard Left Key Is pressed it Works Like A BackSpace Key.
how to write a code in c#.
plz help me.asap.
regards
venkat.
venkat
venkat
|
|
|
|
|
Quick and dirty:
Setup a form with a text box
Add an event to that TextBox for the KeyDown event
Check if the key presses is Keys.Left
If so:
textBox1.text = textBox1.Text.Remove(textBox1.Text.Length - 1)
|
|
|
|
|
Quick and dirty:
Setup a form with a text box
Add an event to that TextBox for the KeyDown event
Check if the key presses is Keys.Left
If so:
textBox1.text = textBox1.Text.Remove(textBox1.Text.Length - 1)
|
|
|
|
|
Hello Friends,
Sealed cannot be inherited but it enables the run-time optimizations
Can anybody specify or explain this point
Thnx
.............
|
|
|
|
|
If you have a class that you don't want somebody to be able to inherit from, mark it as sealed. As the class is marked as sealed, the optimizer can perform certain optimizations such as transform virtual function member invocations into non-virtual invocations.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Thnx ..
But can u explain it more with example .........
|
|
|
|
|
Why? I've explained it and now you have enough information to look it up on google. It's your friend, so use it.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Why is it all these people treat Google like it's the "secret ingredient" that no experienced developer ever mentions in their problem solving recipe??
|
|
|
|
|
I guess that Google's marketing isn't up to much because these idiots seem to have never heard of it.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
I don't know who voted you a one for that. But I've countered it.
|
|
|
|
|
Thanks. I hope it wasn't the marketing guys at Google.
Deja View - the feeling that you've seen this post before.
|
|
|
|