質問

I recently wrote a piece of code like this:

protected void OpenImg_Click(object sender, EventArgs e)
            {
                int i = 0;

                string PathToFolder = "C://LiveSite/img/XL/";

                var dirInfo = new DirectoryInfo(PathToFolder);
                string FileName = Variables.param + "XL.jpg";
                var foundFiles = dirInfo.GetFiles(FileName);

                if (foundFiles.Length == 1)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "openFoundImage", "window.open('" + PathToFolder + foundFiles[i].Name + "');", true);
                }
            }
        }
   }

It pathed correctly but it didn't work because it was pulling off the c:// drive so I changed the location of the image to: http://www.companysite.com/img/XL/ instead. When I replace string PathToFolder = "C://LiveSite/img/XL/"; with http://www.companysite.com/img/XL/ I get the error "URI formats are not supported"

So then I changed my code to:

 protected void OpenImg_Click(object sender, EventArgs e)
                {
                    int i = 0;

                    string uriPath = "http://www.companysite.com/img/XL/";

                    string  localPath = new Uri(uriPath).LocalPath;
                    string FileName = Variables.param + "XL.jpg";
                    var foundFiles = localPath.GetFiles(FileName); //ERROR

                    if (foundFiles.Length == 1)
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "openFoundImage", "window.open('" + uriPath + foundFiles[i].Name + "');", true);
                    }
                }
            }
        }

Now I get an error from using .GetFiles - What am I suppose to use instead of GetFiles? and is my code correct to pull the image from the web? Any help would be appreciated.

役に立ちましたか?

解決

You need to use the webclient class http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx.

This should help you out:

using System;
using System.Net;
using Myapp.Properties;

namespace MyApp
{
    public class Test
    {
        static void Main(string[] args)
        {
        string website ="http://www.fryan0911.com/webclientsample.zip";
        string downloadfolder = Settings.Default.DownloadFolder;

        try
            {
            WebClient wc = new WebClient();
            wc.DownloadFile(website, downloadfolder);
            Console.WriteLine("Web file has been downloaded to " + downloadfolder);
            }
        catch (WebException e)
        {
            Console.WriteLine(e.Message);
        }
        Console.ReadKey();
    }
}
}

code borrowed from:

http://www.fryan0911.com/2009/03/c-download-file-from-website-using-web.html

他のヒント

It looks like you are using files on your own server, then you can grab them like this from codebehind/controller.

        var serverRootPath = Server.MapPath("~");
        var files = Path.Combine(serverRootPath,"img");
        var images = Directory.GetFiles(files);

I was having the same problem.

string app_path = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
string folderpath = Path.GetDirectoryName(app_path);// Error Here

What I noticed is that it provides me with the file path of the assembly, but with "file:///" at the start of the string.

So I did this:

string app_path = System.Reflection.Assembly.GetExecutingAssembly().CodeBase.Replace(@"file:///", "");

Hope this helps.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top