|
I suppose that what you really wanted to ask is: how to read a data with leading zeros and don't loose these zeros.
You have two possibilities:
- Store the number asa string in your application and parse it each time you need it for calculations.
- Create a structure which would store a number of zeros (I would do it this way).
internal struct IntWithZeros
{
private int zerosCount, value;
public static implicit operator int(IntWithZeros x)
{
return x.value;
}
public static IntWithZeros Parse(string data)
{
IntWithZeros ret;
ret.zerosCount = 0;
while (data.Length > ret.zerosCount && data[ret.zerosCount] == '0') {
ret.zerosCount++;
}
ret.value = int.Parse(data);
return ret;
}
public override string ToString()
{
string s = value.ToString();
return s.PadLeft(s.Length + zerosCount, '0');
}
}
Greetings - Jacek Gajek
|
|
|
|
|
private void btnUseLeadingZero_Click(object sender, EventArgs e)
{
int MyNumber = 987;
string MyNumberWithLeadingZero = MyNumber.ToString("00000");
MessageBox.Show(MyNumberWithLeadingZero);
}
|
|
|
|
|
Simplier and better. 5 from me.
Greetings - Jacek Gajek
|
|
|
|
|
Hi guys,
I had just posted a question about getting the path of the current executable. Thanks to all those who responded. However, I found this peice of code while googling just now, and this really seems to work:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
System.Reflection.Assembly a = System.Reflection.Assembly.GetEntryAssembly();
string baseDir = System.IO.Path.GetDirectoryName(a.Location);
Console.WriteLine(baseDir);
Console.ReadLine();
}
}
}
Here, Console.WriteLine(baseDir); prints the path of the .exe.
However, I want that the current executable be copied and moved using the System.IO; .NET class library namespace.
Some thing like this:
Instead of printing the path address with Console.WriteLine(baseDir); , I want to replace it with: System.IO.File.Copy("basedir", "C:\\desiredlocation");
But this code:
System.IO.File.Copy("basedir", "C:\\desiredlocation"); doesn't seem to work.
Can anyone help me out?
|
|
|
|
|
ShreeR.Bhattacharjee wrote: System.IO.File.Copy("basedir", "C:\\desiredlocation");
'basedir' shouldn't be in quotes. And that code snippet will copy the entire directory. Useful if you have references you want preserving, but if you just want to copy the executable, you should be specifying a.Location as the first parameter of File.Copy, not basedir
Between the idea
And the reality
Between the motion
And the act
Falls the Shadow
|
|
|
|
|
Hi Computa,
The case remains same though I apply a.Location as the first parameter of File.Copy method.
Throws exception.
Exception type:
IOException
Exception details:
The target file "C:\\DesiredLocation" is a directory not a file.
Anyway, let me explain the problem more clearly. All I want to do is to copy only the .exe file to C:\\DesiredLocation.
Lemme know, if I need to be more comprehensive.
|
|
|
|
|
Try (new System.Uri(a.CodeBase)).AbsolutePath according to MSDN. This is because the CodeBase property is the fully escaped URL, including the file:// part. File.Copy won't accept this, so you need to create a new Uri, which parses it properly, and use the AbsolutePath property (which gets the proper name)
Between the idea
And the reality
Between the motion
And the act
Falls the Shadow
|
|
|
|
|
I assume that you want to move the file you have referenced in the baseDir variable to the new location - this means you shouldn't put quotes around baseDir in File.Copy, because that's a string literal, not a variable. Change it to
File.Copy(baseDir, @"c:\desiredlocation");
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Hi Pete,
The code:
File.Copy(baseDir, @"c:\desiredlocation"); throws exception!
Exception type:
IOException
Exception Details:
The target file "C:\desiredlocation" is a diretory, not a file.
PS: I want to copy the exe file namely 'ConsoleApplication1.exe' to C:\desiredlocation and not he entire project.
Any ideas?
|
|
|
|
|
You may want to re-read my answer
Between the idea
And the reality
Between the motion
And the act
Falls the Shadow
|
|
|
|
|
Hi Pete,
The code:
File.Copy(baseDir, @"c:\desiredlocation"); throws exception!
Exception type:
IOException
Exception Details:
The target file "C:\desiredlocation" is a diretory, not a file.
PS: I want to copy the exe file namely 'ConsoleApplication1.exe' to C:\desiredlocation and not the entire project.
Any ideas?
|
|
|
|
|
Hey, I read the documentation for you!
public static void Copy (
string sourceFileName,
string destFileName
)
Parameters
sourceFileName
The file to copy.
destFileName
The name of the destination file. This cannot be a directory or an existing file.
Alan.
|
|
|
|
|
Hmm, copy the currently running executable..... we had another guy here recently trying to achieve the same.
|
|
|
|
|
Copying the current execuable is a very difficult task and indeed a difficult task. I dont care who that another guy was and what he wanted to achieve.
|
|
|
|
|
You are the other guy you twat!
|
|
|
|
|
Why do you have to copy the current executable to another location? What kind of application are you writing?
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
This user goes by another name, Rajdeep.Net, who has been repeatidly asking for code or help with intentions to create malicious and/or harmful applications. His previous posts clearly indicate it, and it's undeniable. After so many complaints from users here, he resorted to reregistering under a different name, specifying a different country of origin.
I hate him.
|
|
|
|
|
I had the same doubt as well. I could make it out clearly from the way he forms his query (and also the query itself). Don't give him too much importance to hate him. Just ignore him, he will go away. He has to.
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
ShreeR.Bhattacharjee wrote: Check this out guys!
Give your thread a 'meaningful' title.
It is a crappy thing, but it's life -^ Carlo Pallini
|
|
|
|
|
public class SomeEnumAttribute: DescriptionAttribute
{
public override string Description
{
get
{
string resFile;
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly assembly in assemblies)
{
Type type = assembly.GetType("/*The type defined needs to go in here*/");
if (type != null)
{
resFile = assembly.GetName().Name ;
break;
}
}
}
}
}
public enum MyEnum
{
[SomeEnumAttribute("abc")]
abc = 0,
[SomeEnumAttribute("def")]
def,
[SomeEnumAttribute("ghi")]
ghi,
[SomeEnumAttribute("ghi1")]
ghi1
}
What we ought to do is this: Get the fully qualified name of the enum(MyEnum) and replace it with "/*The type defined needs to go in here*/" in the snippet above.
Can someone help with this.
Note: This class is defined in another binary.
<italics>In another words the question is can we get the enum name(type name) in which the attribute is being used.
You need to google first, if you have "It's urgent please" mentioned in your question.
_AnShUmAn_
modified on Thursday, June 4, 2009 4:11 AM
|
|
|
|
|
typeof(MyEnum).FullName
Between the idea
And the reality
Between the motion
And the act
Falls the Shadow
|
|
|
|
|
Thanks for the reply.
The problem is that this MyEnum is defined elsewhere in another binary and not in this one.
You need to google first, if you have "It's urgent please" mentioned in your question.
_AnShUmAn_
|
|
|
|
|
Then you need to call the code snippet I gave in that binary. If you don't have access to it, the format is namespace.MyEnum . Just make sure you know which namespace it's in
An alternative is to scan the entire assembly, and wait until you get a Type which has IsEnum set, and has a Name of MyEnum. Then, note down the FullName property, and use that when you use Assembly.GetType. Don't do the scanning in your finished program though - just whip up a small testbed to give you the FullName, and use that in your actual code
Between the idea
And the reality
Between the motion
And the act
Falls the Shadow
|
|
|
|
|
Just to make it clear - Are you assuming that you have the name of the Enum, in our case MyEnum, that can be searched in the assembly. The problem still remains on getting the name of the enum in the DescriptionAttribute derived class so that I can use it to locate the assembly in which it is defined.
Thanks for the help...
You need to google first, if you have "It's urgent please" mentioned in your question.
_AnShUmAn_
|
|
|
|
|
I'm still assuming that you want to get the type of MyEnum. The second paragraph shows how you could retrieve the full name. If you already know the full name (including namespace - you may be able to find this out using Red Gate's .Net Reflector), then you can pass this to Assembly.GetType
Between the idea
And the reality
Between the motion
And the act
Falls the Shadow
|
|
|
|