/// <summary> /// Checks if a file exists. /// </summary> /// <param name="path">A <see cref="String"/> representing the path to check.</param> /// <returns>A <see cref="Boolean"/> indicating whether the file exists.</returns> public bool FileExists(string path) { WINAPI.WIN32_FIND_DATA findData = new WINAPI.WIN32_FIND_DATA(); IntPtr hFindFile = WININET.FtpFindFirstFile( _hConnect, path, ref findData, WININET.INTERNET_FLAG_NO_CACHE_WRITE, IntPtr.Zero); try { if (hFindFile == IntPtr.Zero) { return(false); } return(true); } finally { if (hFindFile != IntPtr.Zero) { WININET.InternetCloseHandle(hFindFile); } } }
public bool DirectoryExists(string path) { WINAPI.WIN32_FIND_DATA findData = new WINAPI.WIN32_FIND_DATA(); IntPtr hFindFile = WININET.FtpFindFirstFile( _hConnect, path, ref findData, WININET.INTERNET_FLAG_NO_CACHE_WRITE, IntPtr.Zero); try { if (hFindFile == IntPtr.Zero && Marshal.GetLastWin32Error() != WINAPI.ERROR_NO_MORE_FILES) { return false; } return true; } finally { if (hFindFile != IntPtr.Zero) WININET.InternetCloseHandle(hFindFile); } }
public bool DirectoryExists(string path) { WINAPI.WIN32_FIND_DATA findData = new WINAPI.WIN32_FIND_DATA(); IntPtr hFindFile = WININET.FtpFindFirstFile( _hConnect, path, ref findData, WININET.INTERNET_FLAG_NO_CACHE_WRITE, IntPtr.Zero); try { if (hFindFile == IntPtr.Zero && Marshal.GetLastWin32Error() != WINAPI.ERROR_NO_MORE_FILES) { return(false); } return(true); } finally { if (hFindFile != IntPtr.Zero) { WININET.InternetCloseHandle(hFindFile); } } }
public bool FileExists(string path) { WINAPI.WIN32_FIND_DATA findData = new WINAPI.WIN32_FIND_DATA(); IntPtr hFindFile = WININET.FtpFindFirstFile( _hConnect, path, ref findData, WININET.INTERNET_FLAG_NO_CACHE_WRITE, IntPtr.Zero); try { if (hFindFile == IntPtr.Zero) { return false; } return true; } finally { if (hFindFile != IntPtr.Zero) WININET.InternetCloseHandle(hFindFile); } }
/// <summary> /// Gets details of all directories and their available FTP directory information from the current working FTP directory that match the directory mask. /// </summary> /// <returns>A <see cref="FtpDirectoryInfo[]"/> representing the directories in the current working directory that match the mask.</returns> public FtpDirectoryInfo[] GetDirectories(string path) { WINAPI.WIN32_FIND_DATA findData = new WINAPI.WIN32_FIND_DATA(); IntPtr hFindFile = WININET.FtpFindFirstFile( _hConnect, path, ref findData, WININET.INTERNET_FLAG_NO_CACHE_WRITE, IntPtr.Zero); try { List <FtpDirectoryInfo> directories = new List <FtpDirectoryInfo>(); if (hFindFile == IntPtr.Zero) { if (Marshal.GetLastWin32Error() == WINAPI.ERROR_NO_MORE_FILES) { return(directories.ToArray()); } else { Error(); return(directories.ToArray()); } } if ((findData.dfFileAttributes & WINAPI.FILE_ATTRIBUTE_DIRECTORY) == WINAPI.FILE_ATTRIBUTE_DIRECTORY) { FtpDirectoryInfo dir = new FtpDirectoryInfo(this, new string(findData.fileName).TrimEnd('\0')); dir.LastAccessTime = findData.ftLastAccessTime.ToDateTime(); dir.LastWriteTime = findData.ftLastWriteTime.ToDateTime(); dir.CreationTime = findData.ftCreationTime.ToDateTime(); dir.Attributes = (FileAttributes)findData.dfFileAttributes; directories.Add(dir); } findData = new WINAPI.WIN32_FIND_DATA(); while (WININET.InternetFindNextFile(hFindFile, ref findData) != 0) { if ((findData.dfFileAttributes & WINAPI.FILE_ATTRIBUTE_DIRECTORY) == WINAPI.FILE_ATTRIBUTE_DIRECTORY) { FtpDirectoryInfo dir = new FtpDirectoryInfo(this, new string(findData.fileName).TrimEnd('\0')); dir.LastAccessTime = findData.ftLastAccessTime.ToDateTime(); dir.LastWriteTime = findData.ftLastWriteTime.ToDateTime(); dir.CreationTime = findData.ftCreationTime.ToDateTime(); dir.Attributes = (FileAttributes)findData.dfFileAttributes; directories.Add(dir); } findData = new WINAPI.WIN32_FIND_DATA(); } if (Marshal.GetLastWin32Error() != WINAPI.ERROR_NO_MORE_FILES) { Error(); } return(directories.ToArray()); } finally { if (hFindFile != IntPtr.Zero) { WININET.InternetCloseHandle(hFindFile); } } }
private List <string> List(string mask, bool onlyDirectories) { WINAPI.WIN32_FIND_DATA findData = new WINAPI.WIN32_FIND_DATA(); IntPtr hFindFile = WININET.FtpFindFirstFile( _hConnect, mask, ref findData, WININET.INTERNET_FLAG_NO_CACHE_WRITE, IntPtr.Zero); try { List <string> files = new List <string>(); if (hFindFile == IntPtr.Zero) { if (Marshal.GetLastWin32Error() == WINAPI.ERROR_NO_MORE_FILES) { return(files); } else { Error(); return(files); } } if (onlyDirectories && (findData.dfFileAttributes & WINAPI.FILE_ATTRIBUTE_DIRECTORY) == WINAPI.FILE_ATTRIBUTE_DIRECTORY) { files.Add(new string(findData.fileName).TrimEnd('\0')); } else if (!onlyDirectories) { files.Add(new string(findData.fileName).TrimEnd('\0')); } findData = new WINAPI.WIN32_FIND_DATA(); while (WININET.InternetFindNextFile(hFindFile, ref findData) != 0) { if (onlyDirectories && (findData.dfFileAttributes & WINAPI.FILE_ATTRIBUTE_DIRECTORY) == WINAPI.FILE_ATTRIBUTE_DIRECTORY) { files.Add(new string(findData.fileName).TrimEnd('\0')); } else if (!onlyDirectories) { files.Add(new string(findData.fileName).TrimEnd('\0')); } findData = new WINAPI.WIN32_FIND_DATA(); } if (Marshal.GetLastWin32Error() != WINAPI.ERROR_NO_MORE_FILES) { Error(); } return(files); } finally { if (hFindFile != IntPtr.Zero) { WININET.InternetCloseHandle(hFindFile); } } }
private List<string> List(string mask, bool onlyDirectories) { WINAPI.WIN32_FIND_DATA findData = new WINAPI.WIN32_FIND_DATA(); IntPtr hFindFile = WININET.FtpFindFirstFile( _hConnect, mask, ref findData, WININET.INTERNET_FLAG_NO_CACHE_WRITE, IntPtr.Zero); try { List<string> files = new List<string>(); if (hFindFile == IntPtr.Zero) { if (Marshal.GetLastWin32Error() == WINAPI.ERROR_NO_MORE_FILES) { return files; } else { Error(); return files; } } if (onlyDirectories && (findData.dfFileAttributes & WINAPI.FILE_ATTRIBUTE_DIRECTORY) == WINAPI.FILE_ATTRIBUTE_DIRECTORY) { files.Add(new string(findData.fileName).TrimEnd('\0')); } else if (!onlyDirectories) { files.Add(new string(findData.fileName).TrimEnd('\0')); } findData = new WINAPI.WIN32_FIND_DATA(); while (WININET.InternetFindNextFile(hFindFile, ref findData) != 0) { if (onlyDirectories && (findData.dfFileAttributes & WINAPI.FILE_ATTRIBUTE_DIRECTORY) == WINAPI.FILE_ATTRIBUTE_DIRECTORY) { files.Add(new string(findData.fileName).TrimEnd('\0')); } else if (!onlyDirectories) { files.Add(new string(findData.fileName).TrimEnd('\0')); } findData = new WINAPI.WIN32_FIND_DATA(); } if (Marshal.GetLastWin32Error() != WINAPI.ERROR_NO_MORE_FILES) Error(); return files; } finally { if(hFindFile != IntPtr.Zero) WININET.InternetCloseHandle(hFindFile); } }
/// <summary> /// Gets details of all files and their available FTP file information from the current working FTP directory that match the file mask. /// </summary> /// <param name="mask">A <see cref="String"/> representing the file mask to match files.</param> /// <returns>A <see cref="FtpFileInfo[]"/> representing the files in the current working directory.</returns> public FtpFileInfo[] GetFiles(string mask) { WINAPI.WIN32_FIND_DATA findData = new WINAPI.WIN32_FIND_DATA(); IntPtr hFindFile = WININET.FtpFindFirstFile( _hConnect, mask, ref findData, WININET.INTERNET_FLAG_NO_CACHE_WRITE, IntPtr.Zero); try { List<FtpFileInfo> files = new List<FtpFileInfo>(); if (hFindFile == IntPtr.Zero) { if (Marshal.GetLastWin32Error() == WINAPI.ERROR_NO_MORE_FILES) { return files.ToArray(); } else { Error(); return files.ToArray(); } } if ((findData.dfFileAttributes & WINAPI.FILE_ATTRIBUTE_DIRECTORY) != WINAPI.FILE_ATTRIBUTE_DIRECTORY) { FtpFileInfo file = new FtpFileInfo(this, new string(findData.fileName).TrimEnd('\0')); file.LastAccessTime = findData.ftLastAccessTime.ToDateTime(); file.LastWriteTime = findData.ftLastWriteTime.ToDateTime(); file.CreationTime = findData.ftCreationTime.ToDateTime(); file.Attributes = (FileAttributes)findData.dfFileAttributes; files.Add(file); } findData = new WINAPI.WIN32_FIND_DATA(); while (WININET.InternetFindNextFile(hFindFile, ref findData) != 0) { if ((findData.dfFileAttributes & WINAPI.FILE_ATTRIBUTE_DIRECTORY) != WINAPI.FILE_ATTRIBUTE_DIRECTORY) { FtpFileInfo file = new FtpFileInfo(this, new string(findData.fileName).TrimEnd('\0')); file.LastAccessTime = findData.ftLastAccessTime.ToDateTime(); file.LastWriteTime = findData.ftLastWriteTime.ToDateTime(); file.CreationTime = findData.ftCreationTime.ToDateTime(); file.Attributes = (FileAttributes)findData.dfFileAttributes; files.Add(file); } findData = new WINAPI.WIN32_FIND_DATA(); } if (Marshal.GetLastWin32Error() != WINAPI.ERROR_NO_MORE_FILES) Error(); return files.ToArray(); } finally { if (hFindFile != IntPtr.Zero) WININET.InternetCloseHandle(hFindFile); } }
extern public static int InternetFindNextFile( [In] IntPtr hInternet, [In][Out] ref WINAPI.WIN32_FIND_DATA findData);
extern public static IntPtr FtpFindFirstFile( [In] IntPtr hConnect, [In] string searchFile, [In][Out] ref WINAPI.WIN32_FIND_DATA findFileData, [In] int dwFlags, [In] IntPtr dwContext);