|
Hi,
which methods or namespace i have to use if i like to deal with relative and absolute path ?
i like to use relative path's to a given absolute path
.:Greets from Jerry Maguire:.
|
|
|
|
|
Does System.IO.Path.Combine help?
James
"It is self repeating, of unknown pattern"
Data - Star Trek: The Next Generation
|
|
|
|
|
Hi,
i need it vice versa
i have two absolute paths and like to have the relative path from path1 to path 2, like it's in web hyperlinks.
Any clues ?
.:Greets from Jerry Maguire:.
|
|
|
|
|
Jerry Maguire wrote:
i have two absolute paths and like to have the relative path from path1 to path 2
Ok, I think the easiest thing to do is to break each part down (root, dir, dir, ..., filename) then you can start to remove the like parts one by one until they don't match.
path1 => "/foo/bar/baz/index.html"
path2 => "/foo/bar/stuff/kappa.html"
path1 breakdown:
root = "/"
dir = "foo", "bar", "baz"
filename = "index.html"
path2 breakdown:
root = "/"
dir = "foo", "bar", "stuff"
filename = "kappa.html" Now to generate the relative path...
path1.root == path2.root so remove the root from the list
path1.dir[0] == path2.dir[0] so remove that from the list
path1.dir[1 (really 0 now)] == path2.dir[1 (ditto)] so remove that from the list
path1.dir[2 (really 0 now)] != path2.dir[2 (ditto)] so we can begin to piece together the relative path.
For each directory left in path1 you need to go up one directory (..) then append each of the directories left in path2.
If by chance you have found that all of the directories match then you just need to specify the filename. I'll work up some code then post it as a reply to this msg
James
"It is self repeating, of unknown pattern"
Data - Star Trek: The Next Generation
|
|
|
|
|
OK, I've got the code written
It will probably cause scrolling here, but I've uploaded a text file to my webserver for copy/paste purposes.
http://www.takklesoft.com/GetRelativePath.cs.txt[^]
public string GetRelativePath(string path1, string path2)
{
return GetRelativePath(path1, path2, false, System.IO.Path.DirectorySeparatorChar);
}
public string GetRelativePath(string path1, string path2, bool enableWebChecking)
{
char seperator;
if( enableWebChecking )
seperator = '/';
else
seperator = System.IO.Path.DirectorySeparatorChar;
return GetRelativePath(path1, path2, enableWebChecking, seperator);
}
public string GetRelativePath(string path1, string path2, bool enableWebChecking, char dirSeperator)
{
System.Text.StringBuilder relativePath = new System.Text.StringBuilder();
System.Collections.Specialized.StringCollection path1Dirs;
System.Collections.Specialized.StringCollection path2Dirs;
path1Dirs = new System.Collections.Specialized.StringCollection();
path1Dirs.AddRange(path1.Split( new char [] { dirSeperator } ) );
path2Dirs = new System.Collections.Specialized.StringCollection();
path2Dirs.AddRange(path2.Split( new char [] { dirSeperator } ) );
string path1Root = "";
string path2Root = "";
if( !path1.StartsWith(dirSeperator.ToString()) )
{
path1Root = path1Dirs[0] + dirSeperator.ToString();
path1Dirs.RemoveAt(0);
}
if( !path2.StartsWith(dirSeperator.ToString()) )
{
path2Root = path2Dirs[0] + dirSeperator.ToString();
path2Dirs.RemoveAt(0);
}
if( enableWebChecking )
{
path1Root += path1Dirs[0] + dirSeperator.ToString();
path1Dirs.RemoveAt(0);
path1Root += path1Dirs[0] + dirSeperator.ToString();
path1Dirs.RemoveAt(0);
path2Root += path2Dirs[0] + dirSeperator.ToString();
path2Dirs.RemoveAt(0);
path2Root += path2Dirs[0] + dirSeperator.ToString();
path2Dirs.RemoveAt(0);
}
if( path1Root != path2Root )
return path2;
string filename = "";
path1Dirs.RemoveAt( path1Dirs.Count - 1 );
filename = path2Dirs[ path2Dirs.Count - 1 ];
path2Dirs.RemoveAt( path2Dirs.Count - 1 );
while(
path1Dirs.Count > 0 &&
path2Dirs.Count > 0 &&
path1Dirs[0] == path2Dirs[0] )
{
path1Dirs.RemoveAt(0);
path2Dirs.RemoveAt(0);
}
while( path1Dirs.Count > 0 )
{
relativePath.Append(".." + dirSeperator);
path1Dirs.RemoveAt( 0 );
}
while( path2Dirs.Count > 0 )
{
relativePath.Append(path2Dirs[0] + dirSeperator);
path2Dirs.RemoveAt( 0 );
}
relativePath.Append( filename );
return relativePath.ToString();
} Almost everything I threw at it came out correct so it should work fine for you. [edit]Added a web checking mode so that it properly works on fully qualified URLs (needing the http:// stuff) There is one problem though in that if you use a fully qualified URL on two different servers it won't work correctly (say http://www.foo.com/bar.html and http://www.bar.com/baz.html will return ../../www.bar.com/baz.html .[/edit]
I didn't want to go from fairly generic code to include specific cases so I left it alone, though a quick fix could be added (and I may work on that now )
An enhancement I can think of would be to be smart about creating the relative path. Like if the two absolute paths are /foo/blah/a.html and /b.html have it return /b.html rather than ../../b.html .
James
"It is self repeating, of unknown pattern"
Data - Star Trek: The Next Generation
|
|
|
|
|
Hi,
wow great, thank you
i check it out...
.:Greets from Jerry Maguire:.
|
|
|
|
|
hello everyone,
i have a question, can i convert a bitmap object or any image object to an array of bytes, in order to send it over a LAN ??!!!
if yes, plz tell me how to do it.
P.S. i need this piece of info urgently for my graduation project )
thanks a lot,
bye
|
|
|
|
|
The best way would be saving it directly on the network stream.
|
|
|
|
|
Hi Dalia,
Use the Save method of the Image /Bitmap to save it into a MemoryStream and then open the stream and read it into a byte array.
Hope that helps
Smitha
The future belongs to those who believe in the beauty of their dreams.
--Eleanor Roosevelt
|
|
|
|
|
thanks a lot )
i'm going to try it
|
|
|
|
|
Hi!
I created an appliction with C# to display a large mount of text.
I thought richtextbox control might be a nice choice, but it seems it is not.
I wote my code like this:
public void AppendText(string myMessage)
{
this.richtextbox1.AppendText(myMessage);
}
When I ran my simulation, I got more than 2000 messages coming all the time. Each message is about 2 - 30 rows. The code doesn't work when those 2000 messages were coming. My application was just hanging there and took 100% of CPU.
Then I tried with InvokeMember(). I created a new class called MessageWriter which has a static method called
WriteMessage(string msg). In my form, I changed the code to:
public void AppendText(string myMessage)
{
Type myType = typeof(MessageWriter);
myType.InvokeMember("WriteMessage", BindingFlags.InvokeMethod, null, null, new object[]{myMessage, this.richtextbox1});
}
public static void WriteMessage(string msg, RichTextBox rtb)
{
rtb.AppendText(msg);
}
But unfortunately, this code doesn't work neither. Updating of the richtextbox is far too slow. My application has no time to do anything else.
How can I make richtextbox to update itself quickly! How can I solve this problem?
I am looking forward for your suggestion.
Best regars
Maruko
|
|
|
|
|
Maruko S wrote:
When I ran my simulation, I got more than 2000 messages coming all the time. Each message is about 2 - 30 rows. The code doesn't work when those 2000 messages were coming. My application was just hanging there and took 100% of CPU.
With good reason, you aren't letting the app do anything else. If you want to get some small bit of responsiveness you need to let the application process other messages. To do this put in some calls to Application.DoEvents() in your loop that is processing those 2000 messages.
Maruko S wrote:
Then I tried with InvokeMember().
InvokeMember is just going to slow it down even more, at worst you're incurring another lookup to find the WriteMessage method, at best you have another layer to go through to do make the call to the RichText box's AppendText method.
Maruko S wrote:
How can I make richtextbox to update itself quickly!
My suggestion...batch some of those updates together. Rather than updating 100 times a second (far faster than most people can comprehend) update 3-4 times a second. You can also try Application.DoEvents as I described above.
James
"It is self repeating, of unknown pattern"
Data - Star Trek: The Next Generation
|
|
|
|
|
Hi!
Can anyone please verify that this is indeed a VS .NET bug?
Recipee:
Create a C# Windows Forms project
Add a Menu with a few items
Add an AxMediaPlayer (COM Component)
Place it inside a panel
Bug:
The IDE does not run the Menu. F5 creates the app but
not the menu. In design time, if you click on the Menu icon
in the guiless items tray (bottom), the Form shows the menu.
Click on the form and the menu disapear.
Is it me, heh ???
Thanks !
Antoine
|
|
|
|
|
Some calendars are not supplied in .NET framework.Can somebody guide me ,How to implement custom calender,Any article or library or suggestion?
Mazy
"And the carpet needs a haircut, and the spotlight looks like a prison break
And the telephone's out of cigarettes, and the balcony is on the make
And the piano has been drinking, the piano has been drinking...not me...not me-Tom Waits
|
|
|
|
|
http://www.codeguru.com/forum/showthread.php?s=&postid=746030#post746030
|
|
|
|
|
i have a standard treeview control with checkboxes, how can i check all the children of a node when the parent is checked? i already have events for before and after check so i assume it must go in there but the problem i have is finding which node is selected as a node doesn't have to be selected for it to be checked.
Does anyone have any ideas as its really starting to annoy me as i know it must be so simple
Thanks
|
|
|
|
|
fixed it, i use e.Node to get the children and as its in the event afterCheck, it acts recursively to check all the children!
|
|
|
|
|
check this
TreeView, by recursion you can check anything you want
http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=b650f7fb-147d-4326-8d83-cb51d89a4ca5
|
|
|
|
|
OpenFileDialog type:
I am trying to send Intel hex file to target memory to one of my hardware .
I add (buttonOpenFile) in one of my application to send a file (SendFile) .
The code look like one below.
public void SendFile(FileStream fs)
{
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, (int)fs.Length);
Send(buffer);
}
Now I put the following in my button click handler:
private void buttonOpen_Click(object sender, System.EventArgs e)
{
FileStream fileh = new FileStream(@"C:\Skole\p01.hex", FileMode.Open, FileAccess.Read);
BaseTerm.term.SendFile(fileh);
fileh.Close();
}
The above code work without any problem, but I wish to create an instance of System.Windows .Form OpenFileDialog type
to return a Stream that represents the specified file selected by the user.
--------------------------------------------------------------------------------------
I tried to write the code to do the job, but I do not know how to write the last pieces of code.
I will be grateful if you could help me.
private void buttonOpen_Click(object sender, System.EventArgs e)
{
OpenFileDialog myOpenFileDialog = new OpenFileDialog();
myOpenFileDialog.InitialDirectory = "c:\\Skole\\" ;
myOpenFileDialog.Filter = "hex files (*.hex)|*.hex|All files (*.*)|*.*" ;
myOpenFileDialog.FilterIndex = 1 ;
myOpenFileDialog.RestoreDirectory = true ;
if(myOpenFileDialog.ShowDialog() == DialogResult.OK)
{
Stream myStream = null;
if(( myStream = myOpenFileDialog.OpenFile()) != null)
{
??????????
??????????
}
}
}
Salam Hamid
|
|
|
|
|
Change SendFile to accept a Stream type instead of a FileStream type. Then, pass the stream object received from the dialog-use code.
John :D
|
|
|
|
|
Thanks John it works finally..
Salam
|
|
|
|
|
Hi, all.
I want to paste pictures that have been pasted in Excel.
When I "Copy & Paste" from other tools(MSpaint, MSPhotoEditor etc...),
the format in Clipbord is "Bitmap etc...". So, I can paste it by next code.
Bitmap bmp = (Bitmap)d.GetData(DataFormats.Bitmap)
But, from Excel, the format are "MetafilePict, GIF, PNG, etc... ".
Please help me how to convert(cast) these formats into "Bitmap".
|
|
|
|
|
Hello,
Since I'm a trully begginer in .NET technology, I need advice for
the following problem:
I developed a windows service, who has the meaning to
perform complex processing, over some data. Itself is an
independent application.
The data came from users of a web site (file uploads, or
HTML posts). A web service read the data and save it in files,
on harddisk. The windows service, use FileSystemWatcher to monitor
the arrival of data, and when a new file is there, start to process
it.
The question: is this a good way to implement data exchange
between web service and windows service? I mean, does not
exist other ways to Interprocess-communication/data exchange?
The reason I have concerns is that under heavy stress, the
filesystemwatcher could behave badly, I know it have some inertia...
Does anybody could point me to another kind of "watcher" to use? Some
global events or ... what else??? I really have just a partial knowledge
about .NET capabilities, so any advice is wellcome.
Regards,
Don Miguel.
|
|
|
|
|
If all you're looking for is a different file watching mechanism, you can try this: Store the names of the files you've already processed (if you delete or move the file you just processed, that'll work too). Then, instead of subscribing to the file system notifications, get a directory listing and see which items are new. (If you need to see changed files, store the timestamp and compare those as well.) Now that you have a list of files to process, go ahead and run through them. Update your list of processed files, wait x seconds, and repeat.
If you were wanting to decide on a completely different way of getting the data, you'd need to tell us a lot more information. Do you have control of the application creating the files? Are these on the same machine or separate? How much data is coming through? How often does new data arrive? How critical is it that every single bit of data be processed? Must they be processed in order? Etc.
Have fun finding the answer!
John :D
|
|
|
|
|
Hello John,
First of all, thanks a lot for your time.
You are absolutely right, I can list myself the files which are new,
so I will not depend on file watcher event. (but I really liked the
event notification style...)
But also, I want some advice about different mechanisms to receive data. The application which create the data is on different server, and the single way I thinked to pass data to my windows service was through a file. But first tests, reveal a much lower performance as expected,
so sure another mechanism could help, at least if it bypass harddisk operations.
I don't have control over the application which create the data. I just know it can output a stream of data, in text, xml, or binary. I know the
format. It can send data over network sockets.
As a must, data have to be processed in order. The chalenge for developers, is to find the fastest way for all above communication.
The .NET framework is new for almost all of us, so feedback from
experienced people, which faced real performance issues, is greatly
appreciated.
Thanks again,
Don Miguel
|
|
|
|
|