Assembly.GetExecutingAssembly().Location returns the "wrong" value - c#

I try to use the code below on Monodroid, but "path" is always an empty string.
The interesting thing is that it has already worked, and I don't know what has changed.
The "writeline" below is just for testing purposes, it produces: ": MyDLL.dll: : ".
Last week I updated to mono-android-4.2.7.15330979, but it also worked there.
string path = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
System.Console.WriteLine("{0}: {1}: {2}: {3}", path, Assembly.GetExecutingAssembly().Location, System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), Path.GetDirectoryName(Assembly.GetAssembly(GetType()).Location));
Did I make a mistake?
Is there any known problem with this Assembly functions in mono-android?
EDIT : I also tried this command in std c# in a console application, there it worked!
I found out that the command works, if the project option "Use Fast Deployment(debug mode only)" is enabled. Could this become a problem, when the app is distributed, or built as release?

Related

Directory.Exists(#"\\SERVERIP\aFolder\bFolder"); always returns false

The following path always returns false:
Directory.Exists(#"\\SERVERIP\aFolder\bFolder");
// where SERVERIP is the server-IP which is being accessed using Impersonation
After debugging the code, it places double-slashes in the Debugger.
I have accessed the above file path without the # and double-quotes in WindowsExplorer.
What am I doing wrong?
[ The code will run on a network ]
The problem might be in the paths-[Source/Destinations] (both or one of it[source/destination] might be causing the problem) due to the default-paths used by Visual-Studio. So let me explain how to check wether the paths are correct/incorrect step by step.
Configuring ** SOURCE-PATH **:
Some times this path DRIVE:\ProgramFiles\IISExpress (or some other path depending on the installation location of IIS) gets concatenated with the SOURCE-PATH you give in the input To solve this problem, follow/verify these steps:
Ensure that the SOURCE-PATH or File you are using is in the Project-Folder
To Access the SOURCE-PATH or File. Always use this path/way:
// 1. SOURCE-PATH + fileName with Extension<br>
Server.MapPath("~\FolderInsideProjectFolder\", "fileName.extension");
Configuring ** DESTINATION-PATH (to a Mapped-NETWORK) **:
This path creates a problem if the path you entered has some words mispelled OR if you don't have access to the specified Server-IP[DestinationServerIP]. To solve this problem, follow/verify these steps:
Before Accessing the DESTINATION-PATH or File , ensure that the IP-Address you are referring to is Accessible to the Account under which your Application-code is running.To learn how to run Applications under an Account. See Impersonization
To Access the DESTINATION-PATH or File. Always use this path/way:
// 2. DESTINATION-PATH + fileName with Extension
#"\\SERVERIP\aFolder\bFolder" + "fileName.extension";
NOTE:
Remember that the SOURCE-PATH can be checked if it (exists/does not exist) by addressing its Fully-Qualified-Address and in that case, it will return true if it exists (The full-path that windows-explorer shows you in the Address Bar (Windows-Explorer) like DRIVE:/....../
EXTRA-INFORMATION: (as it was the basic INTENSION)
One line instruction to Copy the file from local-system → networked-mapped drive/path is:
System.IO.File.Copy(
Server.MapPath("~\FolderInsideProjectFolder\", "fileName.extension"),
#"\\SERVERIP\aFolder\bFolder" + "fileName.extension"
[, true ] // Optional if you want the file to be over-written or not
);
Please inform, if any thing still is not cleared (but after some nice searching ☋ ☛ )
Many a times I have seen file (or directory) access problems when the user (a human, system user such as IIS_IUSR or an application) lacks required privileges.
According to this question where the asker is facing similar problem, I believe that this may help you.
Let us know, if it helps.

Viewing Properties.Settings.Default variables

I am trying to use Settings.settings to define/persist some vars. For brevity, I've set up a test file to demonstrate the behavior I'm seeing:
First, I define a setting in Settings.settings:
I then have the following code to test changing variableName:
public Form1()
{
InitializeComponent();
string newString = Properties.Settings.Default.variableName;
Properties.Settings.Default.variableName = "This is a new string";
Properties.Settings.Default.Save();
}
Running the above in the debugger for the first time, I grab the current value (the value I set in the Settings.settings window initially) of variableName from Properties.Settings. As expected, newString is set to "This is a string". Good.....
After executing the next two lines, the debugger shows variableName changed to "This is a new string". Good....
I then run the app through the debugger again. I hit the string newString line and, prior to execution, newString is undefined (of course). Good....
As soon as I execute...
string newString = Properties.Settings.Default.variableName;
... and on subsequent executions of the code, the actual value of variableName is defined as "This is a new string" (Good...as expected).
I then go back to the Settings.settings window. variableName has not changed - it's still "This is a string". I've even closed VSE 2012 and re-opened the project. Settings.settings never changes.
Where is the new value being stored? I've checked all of the .config files ([appname].exe.config, [appname].vshost.exe.config, app.config, and the Settings.settings file) and the new value, "This is a new string" isn't anywhere to be found.
In summary, I'm getting the result I desire from the code, but I can't seem to view the result at design time other than to check the value of the var in the debugger. This seems not only peculiar to me, but impossible.
What am I missing/where am I not looking? I would fully expect the value of variableName to change in the Settings.settings window, but it never does. I've looked everywhere on StackOverflow/Google and can't seem to find the answer.
Thanks in advance....
The original value that you configured via Settings.settings is stored in a .config file alongside your executable's assembly. This will never change unless you modify the Settings file directly via Visual Studio; it's essentially a read-only file.
The user's customized setting is stored in a separate config file within the user's profile. The location of this file depends on your assembly's metadata. For example, on Windows 7/Vista the location might look like:
C:\Users\<user name>\AppData\Local\<company name>\<assembly name>\
AssemblyName\<version>\user.config
If you haven't customized the company name in your assembly's metadata then it may default to Microsoft. Also note that AppData is a hidden folder that may not be visible in Windows Explorer depending on your view settings.
I am not sure if I understand your question. That variable content stay persistent. Thats it. Why you would set a persistent variable to change it later?

How do I get the .exe name of a C# console application?

I'm debugging "xiixtasks.exe", a C# console-mode application in VS2008.
I'm trying to get the version info from xiixtasks.exe.
When I try "Process.GetCurrentProcess()", it gives me the filename and version info for vshost.exe, NOT xiixtasks.exe:
// WRONG: this gives me xiixtasks.vhost.exe, version 9.0.30729.1
// I *want* "xiixtasks.exe", version 1.0.0.1024
System.Diagnostics.FileVersionInfo fi =
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileVersionInfo;
What should I be doing instead?
Thank you in advance!
======================================================
Solution:
1) The initial problem was indeed the IDE's "vshost" wrapper.
One workaround would have been to change the build settings.
2) Assembly.GetExecutingAssembly().CodeBase is an excellent solution - thank you!.
It works inside and outside the debugger.
3) Unfortunately, when I tried calling it with a function that expected a normal file path (instead of a URI like GetExecutingAssembly()" gives you), it died with a "Uri formats are not supported" exception.
4) Final solution: call GetExecutingAssembly(), then Uri.LocalPath ():
...
else if (cmdArgs.cmd.Equals(CmdOptions.CMD_SHOW_VERSION))
{
string codeBaseUri =
Urifile.System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
string codeBase =
new Uri (codeBaseUri).LocalPath;
string sVersion = Util.GetWindowsVersion(codeBase);
System.Console.WriteLine ("version({0}): {1}: ",
Util.Basename(codeBase), sVersion);
}
Thank you once again, all!
Full Path of your assembly:
Assembly.GetExecutingAssembly().CodeBase.Dump();
You can always extract the name with Path.GetFileName:
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
string name = Path.GetFileName(codeBase);
It sounds like you are running inside the IDE in debug with the "Enable the Visual Studio hosting process" checkbox enabled. In which case, the current process is xiixtasks.vshost.exe - it is a shell exe used to help debugging. You need to disable that checkbox.
Try this to get the version:
System.Reflection.Assembly.GetExecutingAssembly().GetName().Version
And this to get the name:
System.Reflection.Assembly.GetExecutingAssembly().GetName().ToString()
Depending on what info you want, you can use a variety of calls:
This would also get you name / version
name: Assembly.GetExecutingAssembly().GetName().Name
version: = Assembly.GetExecutingAssembly().GetName().Version
That's actually what I would expect as it is in fact executing the vshost file in debug mode.
If you don't want it to execute the vshost file but rather your exe directly, you need to go into your project settings and disable the vshost debugging option.
The 'vshost.exe' wrapper is generally only enabled for Debug builds, and it's also not necessary to use it, so perhaps you should just consider turning it off under your project settings (Debug tab, uncheck 'Enable the Visual Studio hosting process' at the bottom).
try
var asm = Assembly.GetExecutingAssembly();
If you will get the exception like "URI format is not supported" While Copying the xml file from one directory to the another by using the following code.
string path=System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
DirectoryInfo dir=new DirectoryInfo(path+"\\App2");
FileInfo[] Files=dir.GetFiles();
then convert it into the URI like.
string path =Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
URI uri = new URI(path);
string FileName = Path.Combine(uri.LocalPath, "\\App2"+file.Name);
then use it to get the files.

C# Using Reshacker to change executable's Icon

There are MULTIPLE threads here asking how to change an icon - and nearly all of them say to use a command line tool such as ResHacker - but none of them (that I have seen) explain how to do so. I read into ResHacker's help file, and I found some text which explained how to go about changing the icon of a win32 executable file.
I tried the below code, and it gave me the following error:
Code:
p.StartInfo.Arguments = "-addoverwrite " + txtProtect.Text + "," + txtProtect.Text + "," + sICOpath + "," + "ICONGROUP" + ", MAINICON, 0";
Error:
"C:\Users\Evan\Desktop\ResHacker.exe" -addoverwrite C:\Users\Evan\Desktop\output.exe,C:\Users\FARINA_EVAN\Desktop\output.exe,C:\Users\Evan\Desktop\ExeWithIcon.exe,ICONGROUP, MAINICON, 0
Error: Invalid resource type.
I realize that this is a bit... Old, but the reason this won't work is because you're trying to get the icon FROM a .exe, which doesn't work with the command line of ResHacker.
The only thing I can think of is to Extract the icon from the .exe and save it as a .ico.
Then you can do the "push."
I wasn't able to find anything on how to programmatically run ResHacker except through manipulating the command line through C# like you are attempting to do. However, to speak to the root of your question, I found a possible solution for you here that does not require ResHacker. Instead, it allows you to modify the icon through code (C# and VB.NET). Here is the link:
http://www.hackforums.net/archive/index.php/thread-422072-1.html

Gems with .NET Applications - How do I set up the Executables so they run without error?

I have a gem, roundhouse, which is an application compiled with .NET (C#). Runs on Windows and it should run in a 32 bit process.
To set up my gemspec, I set:
Gem::Specification.new do |s|
s.platform = 'mswin32'
s.name = 'roundhouse'
s.version = version
s.files = Dir['lib/**/*'] + Dir['bin/**/*']
s.bindir = 'bin'
s.executables << 'rh.exe'
When I install the gem, I should be able to type rh.exe from the command line at any path and it should run correctly.
In practice, I'm not seeing this work correctly. This is what I'm getting back:
Window has this for the header: 16 bit MS-DOS Subsystem
C:\WINDOWS\system32\cmd.exe - rh.exe
The NTVDM CPU has encountered an illegal instruction.
CS:xxxx IP:xxxx OP:xx xx xx xx xx Choose 'Close' to terminate the application.
Here is a picture of the issue (link to TwitPic): Error
If I go to the directory where the item was installed, I can run it and it works great. It's just something in the registration of the command to run from anywhere.
I did quite a bit of searching before asking and came up with nothing. It could be that I don't know what I should be searching for. So let me ask the question, is there a way to register an executable with gems for windows executable applications (built with .NET) and have them register properly with the command line? If so, how is that done?
UPDATE:
I found that gems creates a shim in the C:\Ruby\bin directory that points back to the other file. So there is a rh.exe file that is really just a text file. This is its contents:
#!C:/Ruby/bin/ruby.exe
#
# This file was generated by RubyGems.
#
# The application 'roundhouse' is installed as part of a gem, and
# this file is here to facilitate running it.
#
require 'rubygems'
version = ">= 0"
if ARGV.first =~ /^_(.*)_$/ and Gem::Version.correct? $1 then
version = $1
ARGV.shift
end
gem 'roundhouse', version
load Gem.bin_path('roundhouse', 'rh.exe', version)
if you're distributing it with the file "rh.exe"
then you'll want to create a file
bin/rh
s.executables << 'bin/rh'
then when it's installed gems will create an "rh.bat" file which runs ruby "bin/rh" essentially (as you've seen).
So within bin/rh put something like
result = system(File.dirname(__FILE__) + "/rh.exe" ARGV.join(' '))
exit 1 unless result
result = system(File.dirname(__FILE__) + "/rh.exe " + ARGV.join(' '))
exit 1 unless result
So the endresult should maybe look like? note the space after 'rh.exe'

Categories