|
Is it possible to convert a C++ application (written using VS2005) into C#?
|
|
|
|
|
Yes.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
|
1) Draw a 6-foot circle with white chalk on a red-brick floor.
2) Place an unbroken raw egg in the exact center of the circle.
3) Leave the room for exactly 12 seconds.
4) Re-enter the room.
5) Observe the egg.
6) Something magic happens.
7) SHA-ZAMM! Your C++code will be converted to C#.
There's no magic bullet and no handy utilities to help you. You're actually going to have to be a real programmer and do it by hand.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
John Simmons / outlaw programmer wrote: You're actually going to have to be a real programmer and do it by hand
For some reason, an anatomically inappropriate gesture occurred to me here.;)
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Pete O'Hanlon wrote: an anatomically inappropriate gesture occurred to me here
That's sick.
"Any sort of work in VB6 is bound to provide several WTF moments." - Christian Graus
|
|
|
|
|
Paul Conrad wrote: That's sick.
That's me.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
"Any sort of work in VB6 is bound to provide several WTF moments." - Christian Graus
|
|
|
|
|
Fayu wrote: How?
Take your Object Oriented Design documents ( you know, the ones you created from Analysis of your Requirements and Business Model documentation) and implement the Design in C#. Of course this assumes working knowledge of the .NET platform and the C# language, if you need that first then you might want to start there and then come back to your Design documents.
|
|
|
|
|
No, that's what we like to call a 'rewrite'
Christian Graus - Microsoft MVP - C++
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
I need to access a website and download several hundred spreadsheet files. The problem is that I must use POST to initiate the download. I cannot use webclient.downloadfile becuase I dont have a url to pass.
I am able to get the download started using a bit of test code I wrote. Now I just need to handle the file download dialog that saves the file. If possible I would like to bypass the download dialog and complete the process in code.
The following test code actually works (in its complete form) to get the download started:
object missing = "";
InternetExplorerClass ie = new SHDocVw.InternetExplorerClass();
ie.Navigate("http://alfred.stlouisfed.org/series/downloaddata?seid=RSAFS&cid=6", ref missing, ref missing, ref missing, ref missing);
System.Threading.Thread.Sleep(5000);
mshtml.IHTMLDocument2 doc = (mshtml.IHTMLDocument2)ie.Document;
foreach (mshtml.IHTMLElement pageElement in doc.all)
{
// click some elements
}
Is it possible to get the url that will be sent back to the server from the document AFTER I "click" the parameters?
I have already seen The most complete C# Webbrowser control which is posted here on CodeProject. Either it is not designed to do what I described above or I dont understand it well enough to put it to good use. If there is some code there that illustrates what I am asking, kindly point me in the right direction. Thanks anyone for some help with this. I have searched every corner of the internet for an answer and I am truly stumped.
|
|
|
|
|
Why not use FTP? It's better suited to this type of operation. A simple example would look like this:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
namespace FtpLibrary
{
public class FtpClass
{
#region Members
private string _ipAddress;
private string _userId;
private string _password;
private int _bufferSize;
#endregion
public FtpClass(string ipAddress, string userId, string password)
: this(ipAddress, userId, password, 4096)
{
}
public FtpClass(string ipAddress, string userId, string password, int bufferSize)
{
_ipAddress = ipAddress;
_userId = userId;
_password = password;
if (bufferSize < 1024 || bufferSize > 8192)
_bufferSize = 4096;
_bufferSize = bufferSize;
}
public void Download(string filePath, string fileName)
{
FtpWebRequest ftpRequest;
try
{
string path = Path.Combine(filePath, fileName);
string uri = string.Format("ftp://{0}/{1}", _ipAddress, fileName);
using (FileStream outputStream = new FileStream(path, FileMode.Create))
{
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
ftpRequest.UseBinary = true;
ftpRequest.Credentials = new NetworkCredential(_userId, _password);
FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = _bufferSize;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Hi Pete I would love to use ftp it would solve so many problems. The question I asked really has nothing to do with ftp. Why is the code you posted relevant?
Thanks
|
|
|
|
|
If you don't want to use FTP, then ignore the code; but I put it there to show you how easy it is to use FTP, and to question why you would want to do something other than use FTP to do a task which FTP was designed for. I really can't see why you would want to do anything else here.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Pete,
I appreciate the fact that you have taken the time to respond to my question. If you take a brief moment to read the post you responded to you would note that I am trying to download files from the website of Federal Reserve Bank of St. Louis. While they are a great bunch of folks, I highly doubt that they would put up an ftp server for no other reason than to resolve my little issue .
If you feel the code is useful, please post a specific example of how it might be used in the context I described.
I am open to resolutions of any kind. I have done a fairly extensive search and no one seems to have a solution.
|
|
|
|
|
Is there any particular reason why you wish to automate IE to achieve this instead of the HTTP classes from the base class library?
|
|
|
|
|
Thanks for your suggestion - I will use any method that works. Simpler = better.
I found some examples of using a stream object - I do not understand how to:
a) convert the stream to a mshtml document
b) post the document back to the server
c) handle the subsequent file download.
|
|
|
|
|
How does the server return the Excel document in response to the POST? Does it send it back as a file download, i.e., does the browser show up the file download dialog? Or does it show a link that you can click?
|
|
|
|
|
The browser shows the download dialog (save file as...)
|
|
|
|
|
Here's a sample program that does what you describe. It does a POST to a web page and then gets a file in response which it saves to the local disk. Hope this helps!
using System;
using System.IO;
using System.Net;
using System.Collections.Generic;
using System.Text;
namespace PostDownload
{
class Program
{
static void Main(string[] args)
{
try
{
string url = "http://nerdworks.in/webstuff/getfui.aspx";
string postdata = "fn=logo.gif&url=http%3A%2F%2Fwww.google.com%2Fintl%2Fen_ALL%2Fimages%2Flogo.gif";
WebRequest request = WebRequest.Create(url);
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
((HttpWebRequest)request).Referer = url;
using (Stream stream = request.GetRequestStream())
{
stream.Write(Encoding.UTF8.GetBytes(postdata), 0, Encoding.UTF8.GetByteCount(postdata));
stream.Flush();
}
WebResponse response = request.GetResponse();
using (FileStream dest = new FileStream(GetFilename(response), FileMode.Create))
{
using (Stream src = response.GetResponseStream())
CopyStream(src, dest);
}
}
catch (Exception ex)
{
Console.WriteLine("ERROR: {0}", ex.ToString());
}
}
private static string GetFilename(WebResponse response)
{
string filename = response.Headers["Content-Disposition"];
if (filename != null)
{
string[] tokens = filename.Split(';');
if (tokens.Length == 2)
{
tokens = tokens[1].Split('=');
if (tokens.Length == 2)
filename = tokens[1];
else
filename = null;
}
else
filename = null;
}
if (filename == null)
{
filename = response.Headers["Content-Type"];
if (filename != null)
{
string[] tokens = filename.Split(';');
if (tokens.Length == 2)
{
tokens = tokens[1].Split('=');
if (tokens.Length == 2)
filename = tokens[1];
else
filename = null;
}
else
filename = null;
}
}
if (filename == null)
filename = "ding.dat";
return filename;
}
private static void CopyStream(Stream src, Stream dest)
{
byte[] buffer = new byte[1024];
int read = -1;
while (read != 0)
{
read = src.Read(buffer, 0, buffer.Length);
if (read != 0)
dest.Write(buffer, 0, read);
}
}
}
}
|
|
|
|
|
gleat you are amazing! Thank you very much! I am going to try to make this work. I am sure I will have some questions but I am going to work with this a while first.
|
|
|
|
|
Gleat, thanks again for your program. The only thing that is different in the situation I described is that the postdata values are not known when the program is run. You gave me the info I needed to look that up so all is good.
For the benefit of any one else who is trying to do this:
Download a program called Fiddler from www.fiddler2.com.
Open fiddler then use your web browser to click the elements on the page you are trying to automate. Observe the data posted back to the server. The ParseDocument function you write (see code below) will need to create a string that matches the postdata.
Here is the code you can run to automate your page. Create a form with a textbox and a button. Paste the code below as the form class. The ParseDocument function is specific for my purpose. I included it to demonstrate the basic foreach loop and id test.
<code>
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
try
{
string url = textBox1.Text;
IHTMLDocument2 doc = new HTMLDocumentClass();
WebRequest request1 = WebRequest.Create(url);
WebResponse response1 = request1.GetResponse();
StreamReader reader = new StreamReader(response1.GetResponseStream());
doc.write(reader.ReadToEnd());
doc.close();
response1.Close();
string postdata = ParseDocument(doc);
WebRequest request = WebRequest.Create(url);
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
((HttpWebRequest)request).Referer = url;
using (Stream stream = request.GetRequestStream())
{
stream.Write(Encoding.UTF8.GetBytes(postdata), 0, Encoding.UTF8.GetByteCount(postdata));
stream.Flush();
}
WebResponse response = request.GetResponse();
using (FileStream dest = new FileStream(GetFilename(response), FileMode.Create))
{
using (Stream src = response.GetResponseStream())
CopyStream(src, dest);
}
}
catch (Exception ex)
{
Console.WriteLine("ERROR: {0}", ex.ToString());
}
}
private static string GetFilename(WebResponse response)
{
string filename = response.Headers["Content-Disposition"];
if (filename != null)
{
string[] tokens = filename.Split(';');
if (tokens.Length == 2)
{
tokens = tokens[1].Split('=');
if (tokens.Length == 2)
filename = tokens[1];
else
filename = null;
}
else
filename = null;
}
if (filename == null)
{
filename = response.Headers["Content-Type"];
if (filename != null)
{
string[] tokens = filename.Split(';');
if (tokens.Length == 2)
{
tokens = tokens[1].Split('=');
if (tokens.Length == 2)
filename = tokens[1];
else
filename = null;
}
else
filename = null;
}
}
if (filename == null)
filename = "ding.dat";
return "c:\\" + filename;
}
private static void CopyStream(Stream src, Stream dest)
{
byte[] buffer = new byte[1024];
int read = -1;
while (read != 0)
{
read = src.Read(buffer, 0, buffer.Length);
if (read != 0)
dest.Write(buffer, 0, read);
}
}
public string ParseDocument(IHTMLDocument2 doc)
{
string postdata = "_qf__mainform=";
foreach (mshtml.IHTMLElement pageElement in doc.all)
{
string id = String.IsNullOrEmpty(pageElement.id) ? " " : pageElement.id;
if (id == "selected_vintage_dates")
{
string[] x;
x = pageElement.innerText.Trim().Split(new Char[] {' '});
for (int i = 0; i < x.Length; i++)
{
postdata += "&selected_vintage_dates%5B%5D=" + x[i];
}
break;
}
}
postdata += "&entered_vintage_dates=&file_type=1&file_format=xls&download_data=Download+Data";
return postdata;
}
}
</code>
|
|
|
|
|
hi,
can any body help me while posting the difference between .net framework 1.1 and 2.0?
Thanks
--Krish
prfkrushna@gmail.com
|
|
|
|
|
Many differences.
1- Partial classes in .NET 2.0
2- Generics in 2.0
3- Many more classes.
Check this[^] to get an idea
|
|
|
|
|
hi
i feel it's not that much enough in the interview point of view.
it's better u pls help me some more extent.
otherwise if any written document is there, pls send it to my mail id:
prfkrushna@gmail.com
Thanks
--Krish
prfkrushna@gmail.com
|
|
|
|