示例#1
0
        public static void CreateDirectory(string filename)
        {
            // peel back the path until we find a directory that exists
            string         modPath       = filename;
            Stack <string> directoryList = new Stack <string>();

            while (!Exists(modPath))
            {
                int index1 = modPath.LastIndexOf(Path.DirectorySeparatorChar);
                if (index1 < 0)
                {
                    break;
                }
                else
                {
                    string dirName = modPath.Substring(index1 + 1);
                    directoryList.Push(dirName);
                    modPath = modPath.Substring(0, index1);
                }
            }

            while (directoryList.Count > 0)
            {
                string dirName = directoryList.Pop();
                modPath += Path.DirectorySeparatorChar + dirName;
                PathInternal.DllImport.CreateDirectoryW(PathInternal.ConvertToUnicodePath(modPath), IntPtr.Zero);
            }
        }
示例#2
0
        public static IO.FileStream Open(string filepath, IO.FileMode mode, IO.FileAccess access, IO.FileShare share)
        {
            //opened in the specified mode , access and  share
            IO.FileStream fs      = null;
            uint          umode   = GetMode(mode);
            uint          uaccess = GetAccess(access);
            uint          ushare  = GetShare(share);

            if (mode == IO.FileMode.Append)
            {
                uaccess = PathInternal.DllImport.FILE_APPEND_DATA;
            }

            SafeFileHandle sh     = PathInternal.DllImport.CreateFileW(PathInternal.ConvertToUnicodePath(filepath), uaccess, ushare, IntPtr.Zero, umode, PathInternal.DllImport.FILE_ATTRIBUTE_NORMAL, IntPtr.Zero);
            int            iError = Marshal.GetLastWin32Error();

            if ((iError > 0 && !(mode == IO.FileMode.Append && iError != PathInternal.DllImport.ERROR_ALREADY_EXISTS)) || sh.IsInvalid)
            {
                throw new Exception("Error opening file Win32 Error:" + iError);
            }
            else
            {
                fs = new IO.FileStream(sh, access);
            }
            // if opened in append mode
            if (mode == IO.FileMode.Append)
            {
                if (!sh.IsInvalid)
                {
                    PathInternal.DllImport.SetFilePointer(sh, 0, IntPtr.Zero, PathInternal.DllImport.FILE_END);
                }
            }
            return(fs);
        }
示例#3
0
        private static DateTime GetDateTime(string path, DateTimeAttrib attrib, bool utc)
        {
            PathInternal.DllImport.WIN32_FILE_ATTRIBUTE_DATA fileData = new PathInternal.DllImport.WIN32_FILE_ATTRIBUTE_DATA();
            if (PathInternal.DllImport.GetFileAttributesExW(PathInternal.ConvertToUnicodePath(path), 0, ref fileData))
            {
                switch (attrib)
                {
                case DateTimeAttrib.Write:
                    return(PathInternal.FILETIMEToDateTime(fileData.ftLastWriteTime, utc));

                case DateTimeAttrib.Access:
                    return(PathInternal.FILETIMEToDateTime(fileData.ftLastAccessTime, utc));

                case DateTimeAttrib.Creation:
                    return(PathInternal.FILETIMEToDateTime(fileData.ftCreationTime, utc));

                default:
                    throw new Exception("Unknown DateTimeAttrib in File.GetDateTime() function");
                }
            }
            else
            {
                throw new IO.FileNotFoundException(path);
            }
        }
示例#4
0
 public unsafe static string GetFullPath(string path, bool useUnicodePath)
 {
     fixed(char *buffer = new char[PathInternal.OurMaxPath])
     {
         PathInternal.DllImport.GetFullPathNameW(useUnicodePath ? PathInternal.ConvertToUnicodePath(path) : path, PathInternal.OurMaxPath, buffer, (IntPtr)0);
         return(new string( buffer ));
     }
 }
示例#5
0
 public static bool Remove(string path, bool force)
 {
     if (force)
     {
         PathInternal.RemoveReadOnlyFlag(path);
     }
     return(PathInternal.DllImport.RemoveDirectoryW(PathInternal.ConvertToUnicodePath(path)));
 }
示例#6
0
 public static bool Delete(string filename, bool force)
 {
     if (force)
     {
         PathInternal.RemoveReadOnlyFlag(filename);
     }
     return(PathInternal.DllImport.DeleteFileW(PathInternal.ConvertToUnicodePath(filename)));
 }
示例#7
0
        public static string[] GetFiles(string path, string searchPattern)
        {
            List <string> list = new List <string>();

            PathInternal.DllImport.WIN32_FIND_DATA findData = new PathInternal.DllImport.WIN32_FIND_DATA();
            IntPtr findFileHandle = PathInternal.DllImport.FindFirstFileExW(PathInternal.ConvertToUnicodePath(Path.Combine(path, searchPattern)), 0, ref findData, 0, (IntPtr)0, 0);

            if (findFileHandle.ToInt64() > 0)
            {
                do
                {
                    if ((findData.dwFileAttributes & PathInternal.DllImport.FILE_ATTRIBUTE_DIRECTORY) == 0)
                    {
                        list.Add(Path.Combine(path, findData.cFileName));
                    }
                } while (PathInternal.DllImport.FindNextFile(findFileHandle, ref findData));

                PathInternal.DllImport.FindClose(findFileHandle);
            }

            return(list.ToArray());
        }
示例#8
0
        public unsafe void CopyFile(string sourceFile, string destinationFile)
        {
            mCopyCancelled = false;
            bool success = false;

#pragma warning disable 420
            fixed(bool *cancelPtr = &mCopyCancelled)
            {
                success = CopyFileExW(PathInternal.ConvertToUnicodePath(sourceFile), PathInternal.ConvertToUnicodePath(destinationFile),
                                      new CopyProgressRoutine(this.CopyProgressCallback), IntPtr.Zero, cancelPtr,
                                      COPY_FILE_ALLOW_DECRYPTED_DESTINATION);
            }

#pragma warning restore 420

            //Throw an exception if the copy failed
            if (!success && !mCopyCancelled)
            {
                int error = Marshal.GetLastWin32Error();
                throw new System.ComponentModel.Win32Exception(error);
            }
        }
示例#9
0
 public static bool Move(string source, string dest)
 {
     return(PathInternal.DllImport.MoveFileW(PathInternal.ConvertToUnicodePath(source), PathInternal.ConvertToUnicodePath(dest)));
 }
示例#10
0
 public static bool SetFileAttributes(string filename, UInt32 attribs)
 {
     return(PathInternal.DllImport.SetFileAttributesW(PathInternal.ConvertToUnicodePath(filename), attribs));
 }
示例#11
0
 public static UInt32 GetFileAttributes(string filename)
 {
     return(PathInternal.DllImport.GetFileAttributesW(PathInternal.ConvertToUnicodePath(filename)));
 }