使用C#自动化文件下载

时间:2019-04-25 12:10:53

标签: c#

我的要求是从网站上自动下载文件,但是,我必须处理“文件下载窗口”,而且我想不出任何办法来克服它。

我想将文件下载到我提供的自定义路径中,而不是下载到默认目录中。

这是我的代码:

class YieldPortal
    {
        private string yieldPortalPath;
        public Dictionary<string, string> productDetail;
        private WebBrowser myWebBrowser;
        private HtmlElementCollection theElementCollection;
        private int loadIndex;
        public YieldPortal()
        {
            yieldPortalPath = "http://portal.sgp.company.org/Content/Master/product.aspx";
            productDetail = new Dictionary<string, string>();
            myWebBrowser = new WebBrowser();
            myWebBrowser.AllowNavigation = true;
            loadIndex = 0;
            myWebBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
        }
        WinEventDelegate dele = null;
        delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);

        [DllImport("user32.dll")]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll")]
        static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, IntPtr lParam);

        [DllImport("user32.dll")]
        static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);

        [DllImport("user32.dll")]
        static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll")]
        static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern uint GetCurrentThreadId();

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr SetActiveWindow(IntPtr hWnd);

        [DllImport("user32")]
        public static extern uint GetDlgCtrlID(IntPtr hwnd);

        //public const uint EVENT_SYSTEM_FOREGROUND = 3;
        public const uint WINEVENT_OUTOFCONTEXT = 0;
        static WinEventDelegate procDelegate = new WinEventDelegate(WinEventProc);
        public void populate_productDetail()
        {
            myWebBrowser.Navigate(yieldPortalPath);

        }

        private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            theElementCollection = myWebBrowser.Document.GetElementsByTagName("input");
            foreach (HtmlElement curElement in theElementCollection)
            {
                if (curElement.GetAttribute("name").ToString() == "btnExport")
                {
                    curElement.InvokeMember("click");
                    IntPtr hhook = SetWinEventHook(32770, 32770, IntPtr.Zero, procDelegate, 0, 0, WINEVENT_OUTOFCONTEXT);
                    StringBuilder ClassName = new StringBuilder(100);
                    GetClassName(hhook, ClassName, ClassName.Capacity);
                    if (hhook != IntPtr.Zero)
                     {
                     }

                    break;
                }
            }
        }

        static void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
        {
            const uint OBJID_WINDOW = 0;
            const uint CHILDID_SELF = 0;

            // filter out non-HWND, and things not children of the current application
            if (idObject != OBJID_WINDOW || idChild != CHILDID_SELF)
                return;

            //Get the window class name
            StringBuilder ClassName = new StringBuilder(100);
            GetClassName(hwnd, ClassName, ClassName.Capacity);

            // Send close message to any dialog
            if (ClassName.ToString() == "#32770")
            {
                IntPtr hds = FindWindow("#32770", "File Download");
                IntPtr hokBtn = FindWindowEx(hds, IntPtr.Zero, "Button", "&Save");
                uint id = GetDlgCtrlID(hokBtn);
                SetActiveWindow(hds);
                IntPtr res = SendMessage(hokBtn, (int)0x00F5, 0, IntPtr.Zero);
                Thread.Sleep(500);
                if (res.ToInt32() == 1)
                    MessageBox.Show("success");
            }
            if (ClassName.ToString() == "#32768")
            {
                MessageBox.Show("Hello 2");
            }
        }
    }

public YieldPortal myYieldPortal;
myYieldPortal = new YieldPortal();
myYieldPortal.populate_productDetail();

在上面的代码中,“ res”为零,而hds和hokBtn不为零。我可以知道如何纠正这个问题吗?

另一方面,如何获取另存为窗口的句柄并保存到自定义路径?

0 个答案:

没有答案