It is possible to read files located on any hosts?

Discussion in 'Visual Studio' started by wwv, Jun 15, 2006.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. wwv

    wwv

    Hello,</o:p></o:p>It is possible to read with Visual Studio 2003 / 2005 files located on anywhere on any hosts ?</o:p></o:p>I wrote the following line code with both Visual 2003 and 2005 :</o:p></o:p>
    File.Open ("http://www.wwv-it.de/Statistic1.txt", FileMode.Open, FileAccess.Read,FileShare.Read); </o:p>
    </o:p>
    I got the following error:</o:p>
    Exception Details: System.ArgumentException: URI formats are not supported.</o:p>
    </o:p>
    Thank you in advance for your answers!</o:p>
    </o:p>
    Valentin</o:p>
     
  2. Bruce

    Bruce DiscountASP.NET Staff

    No...

    File.Open() and any file. methods only access files on the server hosting the site.

    Bruce

    DiscountASP.NET
    www.DiscountASP.NET
     
  3. wwv

    wwv

    Hi Joel,</o:p>
    </o:p>
    Thanks a lot!</o:p>
    I have converted your Visual Basic class to Visual C#.</o:p>
    It works fine.</o:p>
    I use that class / methods to read text files derived from Excel maps (.csv) or Word tables. </o:p>
    Then I convert the data to DataSets for uploading in database tables.</o:p>
    I have enclosed the Visual C# class.</o:p>
    </o:p>
    Regards,</o:p>
    </o:p>
    Valentin </o:p>
    </o:p>
    www.wwv-it.eu</o:p>
    </o:p>
    using System;</o:p>
    using System.Net;</o:p>
    using System.IO;</o:p>
    </o:p>
    // Usage example : TextBox1.Text = EasyHttp.Send ("http://www.wwv-it.de/Statistic1.txt", "", EasyHttp.HTTPMethod.HTTP_GET, "");</o:p>
    </o:p>
    namespace HttpSendReceive</o:p>
    {</o:p>
    </o:p>
    public class EasyHttp</o:p>
    {</o:p>
    public EasyHttp()</o:p>
    {</o:p>
    }</o:p>
    </o:p>
    public enum HTTPMethod</o:p>
    {</o:p>
    HTTP_GET= 0,</o:p>
    HTTP_POST = 1</o:p>
    }</o:p>
    </o:p>
    public static string Send(string URL, string PostData, HTTPMethod Method, string ContentType)</o:p>
    {</o:p>
    HttpWebRequest Request = (HttpWebRequest) WebRequest.Create(URL);</o:p>
    HttpWebResponse Response; </o:p>
    StreamWriter SW; </o:p>
    StreamReader SR = null; </o:p>
    string ResponseData; </o:p>
    </o:p>
    // Prepare Request Object</o:p>
    Request.Method = Method.ToString().Substring(5);</o:p>
    </o:p>
    // Set form/post content-type if necessary</o:p>
    </o:p>
    if (Method == HTTPMethod.HTTP_POST &amp;&amp; PostData != "" &amp;&amp; ContentType == "")</o:p>
    ContentType = "application/x-www-form-urlencoded";</o:p>
    </o:p>
    // Set Content-Type</o:p>
    if (ContentType != "")</o:p>
    {</o:p>
    Request.ContentType = ContentType;</o:p>
    Request.ContentLength = PostData.Length;</o:p>
    }</o:p>
    </o:p>
    </o:p>
    // Send Request, if Request</o:p>
    if (Method == HTTPMethod.HTTP_POST)</o:p>
    {</o:p>
    SW = new StreamWriter(Request.GetRequestStream());</o:p>
    try</o:p>
    {</o:p>
    SW.Write(PostData);</o:p>
    }</o:p>
    catch (System.Exception ex)</o:p>
    {</o:p>
    throw ex;</o:p>
    }</o:p>
    </o:p>
    finally</o:p>
    {</o:p>
    SW.Close();</o:p>
    }</o:p>
    </o:p>
    }</o:p>
    </o:p>
    </o:p>
    // Receive Response</o:p>
    try</o:p>
    {</o:p>
    Response = (HttpWebResponse) Request.GetResponse();</o:p>
    SR = new StreamReader(Response.GetResponseStream());</o:p>
    ResponseData = SR.ReadToEnd();</o:p>
    }</o:p>
    catch (System.Net.WebException Wex)</o:p>
    {</o:p>
    </o:p>
    SR = new StreamReader(Wex.Response.GetResponseStream());</o:p>
    ResponseData = SR.ReadToEnd();</o:p>
    throw new Exception(ResponseData);</o:p>
    }</o:p>
    finally</o:p>
    {</o:p>
    SR.Close();</o:p>
    }</o:p>
    </o:p>
    </o:p>
    return ResponseData;</o:p>
    </o:p>
    }</o:p>
    </o:p>
    }</o:p>
    }</o:p>
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page