|
thank you very much . I will try it right away.
|
|
|
|
|
I have done it , but there is some problems.
I think it's the problem about privilege to read file and I have solve it by another method.
To my surprise , I can read some files and can't read the others . There is no difference between these files .
Can you tell me why it's so ? Thanks a lot.
|
|
|
|
|
i have an array of strings that represent filenames with the path. i want to find the last directory that is common between all the files.
ex:
c:\dir1\dir2\abc\abcd\file.ext
c:\dir1\dir2\abc\efgh\file.ext
c:\dir1\dir2\abc\efgh\123\file.ext
the last common directory would be c:\dir1\dir2\abc\
what would be the easiest way to go about this?
thanks,
Rob Tomson
--
There are 10 kinds of people. Those who understand binary and those who don't.
|
|
|
|
|
The easiest way would be to post your question and have some write the code for you.
Here is the code:
private string FindSame(params string[] values)
{
// Default to nothing
string sAns = null;
// Start out before any valid value
int iSame = -1;
// Do we have any strings?
if (values.Length > 0)
{
// Setup for looping
bool bLoop = true;
// Holding area
char cChecking = ' ';
// Flag
bool bCheckingValid;
// Loop
while (bLoop)
{
// Move on to next character
iSame++;
// Reset flag
bCheckingValid = false;
//Loop thru the strings
for (int iValue = values.GetLowerBound(0); iValue <= values.GetUpperBound(0); iValue++)
{
// Reached the end?
if (iValue >= values[iValue].Length)
{
// No more soup for you
bLoop = false;
}
else
{
// Do we have something to compare against?
if (!bCheckingValid)
{
// Pick up comparison
cChecking = values[iValue][iSame];
// Flag that we got a value
bCheckingValid = true;
}
else
{
// Compare to string
if (cChecking != values[iValue][iSame])
{
// Mismatch!
bLoop = false;
}
}
}
}
}
// Get the string
sAns = values[values.GetLowerBound(0)].Substring(0, iSame);
}
// Return
return sAns;
}
and here is a sample call:
string sSame = this.FindSame(
@"c:\dir1\dir2\abc\abcd\file.ext",
@"c:\dir1\dir2\abc\efgh\file.ext",
@"c:\dir1\dir2\abc\efgh\123\file.ext");
|
|
|
|
|
There are a few ways to better the performance, but that's an exercise for the original poster.
I just wanted to add that you should either use Char.ToLower before comparing, or something similar. Currently, the code won't recognize the following case (and similar cases): @"c:\dir1\file1.ext" and @"C:\DIR1\FILE2.ext" (even one difference in case would cause this to fail). Paths in Windows are case-insensitive.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Open Reply to your Private Reply:
I agree with you that a lot of people that are interested in becoming a programmer (and even many that "are") do noy belong in this field.
Like in any field, you need the drive to look for answers yourself and to read the manual (how old is RTFM now?) and to dig in deeper, and too many are plain too lazy to do so.
However, I have met many who do have the drive, and the ability is within them, they just lack that insight of thinking outside themselves, which may be (AND IT IS A BIG MAY BE) triggered by seeing a piece of code.
I do not know about you, you may have all the answers and need help from no one, but I have gotten great ideas from reading thru some of these boards (the latest example is the work by Tom Clements on menus here at CP, which gave me insight on how to solve a nasty problem I was facing), and provide the little assistance I humbly do to pay back into the system.
Once before I answered someone and it seemed to cause some problem with you, and I apologized, even when I felt that I had done nothing wrong. This time you are telling me not to help people out, and to that my answer is to go play with yourself. I do not believe that this board belongs to you, and if it does, publicly tell everyone so and I will happily go someplace else.
|
|
|
|
|
Thank you very much. Sorry it took so long to reply but anyway...I took your basic idea and massaged it abit and came up with this:
<br />
private int FindCommonDirectory(string[] files)<br />
{<br />
string path = "";<br />
foreach (string file in files)<br />
{<br />
string filePath = file.Substring(0, file.LastIndexOf(dirSep) + 1);<br />
if ("" == path) path = filePath;<br />
else if (filePath == path){}<br />
else if (!filePath.StartsWith(path))<br />
{<br />
for (int chr = 0; chr <= filePath.Length; chr++)<br />
{<br />
if (filePath[chr] != path[chr])<br />
{<br />
path = filePath.Substring(0, chr);<br />
break;<br />
}<br />
}<br />
}<br />
}<br />
return path.LastIndexOf(dirSep);<br />
}<br />
Basically what it does is gets the directory of the first item and then compares it to the rest of the items, char by char. Every time it finds a shorter directory it then uses that to compare with the rest.
Thanks again for your help,
Rob
--
There are 10 kinds of people. Those who understand binary and those who don't.
|
|
|
|
|
You're welcome. The idea was to give an example, and let you run with it.
Next, pass it forward, when you see a question that you have an answer for, give them a hand.
|
|
|
|
|
hello
can anyone tell me how to clear
or reset to the default value of the
combo box in winforms?
|
|
|
|
|
I'm not sure if you want to delete all items, or just reset the text.
To delete all items you do:
combobox.Items.Clear()
to set a value displayed:
combobox.Text = "text value";
|
|
|
|
|
Hi,
how can I get the number of running threads on the machine. Not just the ones I have created but all threads.
I have googeled a little but didn't find anything useful ...
Q:What does the derived class in C# tell to it's parent?
A:All your base are belong to us!
|
|
|
|
|
You could use Process.GetProcesses to get all the processes on the system, enumerate them, and use Process.Threads.Count to tally a count. Depending on your privileges, though, this might not represent all the threads in the system (only those for which you have access to the processes).
There's also a way using WMI, but it's more difficult to implement and will be slower. Besides, your credentials are typically used when accessing the WMI host so you'll most likely run into the problem described above.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanx, I will look into that.
Q:What does the derived class in C# tell to it's parent?
A:All your base are belong to us!
|
|
|
|
|
|
Nice find...I just wish they would have had that up a few years ago .
I, for one, do not think the problem was that the band was down. I think that the problem may have been that there was a Stonehenge monument on the stage that was in danger of being crushed by a dwarf.
-David St. Hubbins
|
|
|
|
|
Cool!
But I need an interface similar to "Api Viewer" of the Visual Basic 6.0.
Any suggestions?
Alexsander "Axia" Antunes
|
|
|
|
|
Hia!
I need to hide the border of a win button keeping at the same time the text of it. I think tha the best way to solve my problem is to asign the Form color to the button´s border but there is the problem:
It´s possible to change the border color of a Win Button without changing at the same time the color of the text shown?
By now i´ve only found 2 variables to manage that stuff (ForeColor & BackColor), the ForeColor manage both tge text and the border.
Can somebody help me?
Thanx in advance.
|
|
|
|
|
Why don't you just set Button.FlatStyle to FlatStyle.Flat ?
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks Heath, but the flat style include the border too.
The problem is that the border color and the text color are controlled by the same property, no matter the FlatStyle selected.
I think that i need to override a Paint method or something similar.
Cheers!
|
|
|
|
|
I'm trying to run the GetDisplayName method of a folder (in this case, the desktop folder). It all executes perfectly as far as I can see, until it reaches the StrRetToStr method, where it crashes with a null pointer error.
I'm slightly confused as its practicaly identical to the tutorial on this site, and all values forwarded are allocated succesfuly by previous functions.
The code follows:
IntPtr desktopPIDL = new IntPtr();<br />
<br />
COMStuff.SHGetDesktopFolder(out desktopPIDL);<br />
<br />
Type shellType = typeof(IShellFolder);<br />
<br />
IShellFolder shellFolder = (IShellFolder)<br />
Marshal.GetTypedObjectForIUnknown(desktopPIDL, shellType);<br />
<br />
<br />
IntPtr interfacePtr = new IntPtr();<br />
IntPtr relativePIDL = new IntPtr();<br />
<br />
Guid myGUID = Marshal.GenerateGuidForType(shellType);<br />
<br />
result = COMStuff.SHBindToParent(desktopPIDL,<br />
myGUID,
out interfacePtr,<br />
out relativePIDL);<br />
<br />
IShellFolder myShellFolder = (IShellFolder)Marshal.GetTypedObjectForIUnknown(interfacePtr,shellType);<br />
<br />
mySTRRET = new STRRET();<br />
result = myShellFolder.GetDisplayNameOf(relativePIDL,SHGDN_Flags.SHGDN_NORMAL | SHGDN_Flags.SHGDN_FORPARSING<br />
,ref mySTRRET);<br />
<br />
<br />
myFileName = new string(' ',128);<br />
<br />
result = COMStuff.StrRetToStr(mySTRRET, relativePIDL, out myFileName);
The calling code above gets to the last line before crashing.. STRRET has all it's values set to the same PIDL, which is expected, as the type is identifiened by the type value. However, the type value remains 0 regardless of the flags on the STRRET structure returned by the GetDisplayName of.
The STRRET struct and the StrRetToStr method are declared as follows:
<br />
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Auto)]<br />
public struct STRRET<br />
{<br />
[FieldOffset(0)]<br />
public uint uType;
<br />
[FieldOffset(4)]<br />
public IntPtr pOleStr;
<br />
[FieldOffset(4)]<br />
public UInt32 uOffset;
<br />
[FieldOffset(4)]<br />
public IntPtr cStr;
<br />
}<br />
<br />
<br />
<br />
[DllImport("shlwapi.dll", CharSet=CharSet.Auto)]<br />
public static extern int StrRetToStr(<br />
ref STRRET pstr,<br />
IntPtr pidl,<br />
[MarshalAs(UnmanagedType.BStr)]<br />
out string pbstring);<br />
I'm certain that the error lies in either the declaration of my STRRET structure, or the implementation of StrRetToStr.
I'm going to experiment with the alternative method as well, StrRetToBuf, but I susspect a similar problem will occur.
If anyone could shed some light on this subject, it would be most grateful.
regards
Cata
|
|
|
|
|
You're not calling StrRetToStr with a ref . A struct - like I've said to you many times - is a value type. StrRetToStr requires it's address, so either use ref when declaring AND PASSING the parameter, or use GCHandle to get the address and free it when finished.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
My bad, I copied and pasted some code i had beed playing with and edited the ref out of, I am actualy calling it as a Ref. Like this:
result = myShellFolder.GetDisplayNameOf(relativePIDL,SHGDN_Flags.SHGDN_NORMAL | SHGDN_Flags.SHGDN_FORPARSING<br />
,ref mySTRRET);<br />
<br />
<br />
myFileName = new string(' ',128);<br />
<br />
result = COMStuff.StrRetToStr(ref mySTRRET, relativePIDL, out myFileName);
And it's still not working, the object is assigned a value to the union members, but it's type value remains 0. I'm not sure, if this is a problem with the strucure or the GetDisplayNameOf method.
Cata
|
|
|
|
|
Don't use out for the last parameter. A String is already a reference type. Also, when alloc'ing your myFileName variable, use '\0' (null) instead of a space. This makes sure that everything is properly null-terminated in case StrRetToStr does something differently.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I see, thanks Heath, it's running without incident now. However, my string is still unasigned after the StrRet call. The STRRET still comes out of the getName function with a type value of 0, and i'm sure that's wrong.
I played around with setting it manualy, but for values 1 and 2, it crashes, and for 3 I get a null string. I didn't think it would work, but had to try.
I can only think it would be the GetName method that's not working properly....
What are your thoughts?
Cata
|
|
|
|
|
Is there an easy way to draw a text on a path? Specifically if I have a curve and I want this text to go along that curve, how can I do that?
Thanks.
|
|
|
|