1

我的 C# 项目中的 FtpFindFirstFile 函数有问题。基本上这个函数只是在指定目录中搜​​索我在程序中提到的文件,但是在函数执行完成之前会出现错误,这是错误的屏幕截图:

---------------开始代码------

[System.Runtime.InteropServices.DllImport("wininet.dll", EntryPoint = "InternetOpen", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
static extern IntPtr InternetOpen(
              string lpszAgent, int dwAccessType, string lpszProxyName,
              string lpszProxyBypass, int dwFlags);
[System.Runtime.InteropServices.DllImport("wininet.dll", EntryPoint = "InternetConnect", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)] 
extern public static IntPtr  /*IntPtr*/ InternetConnect(
         IntPtr hInternet, string lpszServerName, int nServerPort,
         string lpszUsername, string lpszPassword, int dwService,
         int dwFlags, int dwContext);

    [System.Runtime.InteropServices.DllImport("wininet.dll", EntryPoint = "FtpFindFirstFile", SetLastError = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    extern public static IntPtr FtpFindFirstFile(
        IntPtr hConnect, string searchFile, out WIN32_FIND_DATA findFileData,
        int flags, IntPtr context); 

    #region WIN32_Structure

    public struct WIN32_FIND_DATA
    {
        public int dwFileAttributes;
        public int nFileSizeHigh;
        public int nFileSizeLow;
        public int dwReserved0;
        public int dwReserved1;
        public string cFileName; 
        public string cAlternateFileName;
    }
    #endregion

    public void PerformFTP(string HostIP, string logUsrName, string LogPwd, string SendType, string DefaultDir, string fileExtension)
    {
        #region Declaration
        WIN32_FIND_DATA win32 = new WIN32_FIND_DATA();
        bool pRoceed;
        #endregion
        pRoceed = true;

        /*  Initialize Internet Connection */
        IntPtr hInternet = InternetOpen("browser", INTERNET_OPEN_TYPE_DIRECT, null, null, 0);
        //IntPtr hInternet = InternetOpen("browser", 1, null, null, 0);

        if (hInternet == IntPtr.Zero)
        {
            MessageBox.Show(hInternet.ToString(), "");
            MessageBox.Show(System.Runtime.InteropServices.Marshal.GetLastWin32Error().ToString()); 
        }

        /*  Initialize FTP Connection  */
        IntPtr hFTPhandle = InternetConnect(hInternet, HostIP, INTERNET_DEFAULT_FTP_PORT, logUsrName, LogPwd, INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
        //IntPtr hFTPhandle = InternetConnect(hInternet, "203.177.252.123", 21, "bomoracle", "bomoracle", 1, 0, 0);

        /*  To check if the FTP connection succeeded */
        if (hFTPhandle == IntPtr.Zero)
        {
            pRoceed = false;
            MessageBox.Show(hFTPhandle.ToString(), "");
            MessageBox.Show(System.Runtime.InteropServices.Marshal.GetLastWin32Error().ToString());
            return;
        }

        //IntPtr hFind = FtpFindFirstFile(hFTPhandle, "*.DAT" /*+ fileExtension*/ , out win32, 0, IntPtr.Zero);

        IntPtr hFind = FtpFindFirstFile(hFTPhandle, "*.DAT"  , out win32, 0, IntPtr.Zero); **//THIS IS WHERE THE ERROR APPEARS**

        if (hFind == IntPtr.Zero)
        {
            if (System.Runtime.InteropServices.Marshal.GetLastWin32Error().ToString() == "RROR_NO_MORE_FILES") 
            {
                MessageBox.Show("NO MORE .BOM FILES","EMPTY");
            }
            MessageBox.Show("SEARCHING IN THE DIRECTORY FAILED! ", "EMPTY");
        }

    }

---------------结束代码------------------

这是错误消息,它出现在执行 if-else 条件之前:

“试图读取或写入受保护的内存。这通常表明其他内存已损坏。”

我不知道是什么导致了错误,我只是在搜索目录,我还没有对那个 ftp 进程执行任何 get 或 put 命令。希望你能帮忙!谢谢!

4

1 回答 1

1

我无法回答您的具体问题,但我强烈认为已经有一个托管解决方案。请记住,只有当框架没有满足您需求的实现时,您才需要回退到互操作。

var request = (FtpWebRequest)WebRequest.Create("ftp://example.com/");
request.Credentials= new NetworkCredential("username", "password");
// List files
request.Method = WebRequestMethods.Ftp.ListDirectory;

var resp = (FtpWebResponse) request.GetResponse();
var stream = resp.GetResponseStream();
var readStream = new StreamReader(resp.GetResponseStream(), System.Text.Encoding.UTF8);

// handle the incoming stream, store in a List, print, find etc
var files = new List<String>();
if (readStream != null)
{
    while(!readStream.EndOfStream)
    {
      files.Add(readStream.ReadLine());
    }
} 
// showe them
foreach(var file in files)
{
   Console.WriteLine(file);
}
// find one
var fileToFind = "Public";
var foundFile = files.Find( f => f == fileToFind);
Console.WriteLine("found file {0}:", foundFile);
// show status
Console.WriteLine("List status: {0}",resp.StatusDescription); 

在这个片段中我使用了:
FtpWebResponse
FtpWebRequest
WebRequestMethods.Ftp
List.Find
StreamReader

于 2013-10-21T14:29:51.157 回答