Exemplo n.º 1
0
        //
        // Get content stream for a given source file, the content could
        // come from memory or disk based on the build host environment.
        //
        // It is the caller's responsibility to close the stream.
        //
        public Stream GetContent(string srcFile)
        {
            Stream fileStream = null;

            if (String.IsNullOrEmpty(srcFile))
            {
                throw new ArgumentNullException("srcFile");
            }

            if (HostFileManager != null)
            {
                //
                // Build Host environment has a FileManager, use it to get
                // file content from edit buffer in memory.  GetFileContents
                // removes the BOM before returning the string.
                //
                string strFileContent = HostFileManager.GetFileContents(srcFile);


                // IVsMsBuildTaskFileManager.GetFileContents should never return null.
                // GetBytes might throw when input is null, but that should be fine.
                //
                // For xaml file, UTF8 is the standard encoding
                //
                UTF8Encoding utf8Encoding  = new UTF8Encoding();
                byte[]       baFileContent = utf8Encoding.GetBytes(strFileContent);
                fileStream = new MemoryStream(baFileContent);
            }
            else
            {
                fileStream = File.OpenRead(srcFile);
            }

            return(fileStream);
        }
Exemplo n.º 2
0
        //
        // Save content stream for the given destination file
        // UTF8 BOM should not be added to the contentArray.  This
        // method adds BOM before writing file to disk.
        //
        public void WriteFile(byte[] contentArray, string destinationFile)
        {
            if (String.IsNullOrEmpty(destinationFile))
            {
                throw new ArgumentNullException("destinationFile");
            }

            if (contentArray == null)
            {
                throw new ArgumentNullException("contentArray");
            }

            UTF8Encoding utf8Encoding = new UTF8Encoding();
            string       contentStr   = utf8Encoding.GetString(contentArray);

            if (IsFileInHostManager(destinationFile))
            {
                // PutGeneratedFileContents adds BOM to the file when saving
                // to memory or disk.
                HostFileManager.PutGeneratedFileContents(destinationFile, contentStr);
            }
            else
            {
                // Add BOM for UTF8Encoding since the input contentArray is not supposed to
                // have it already.
                using (StreamWriter sw = new StreamWriter(destinationFile, false, new UTF8Encoding(true)))
                {
                    sw.WriteLine(contentStr);
                }
            }
        }
Exemplo n.º 3
0
        private void ReadHosts()
        {
            HostFileManager.FileOperationResult readResult;
            this.hosts = HostFileManager.ReadHosts(out readResult);
            switch (readResult)
            {
            case HostFileManager.FileOperationResult.UnauthorizedAccess:
                MessageBox.Show(Resources.msg_error_unauthorized_user, Resources.error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                break;

            case HostFileManager.FileOperationResult.FileNotFound:

                DialogResult userChoice = MessageBox.Show(Resources.msg_warning_host_file_not_found, Resources.warning, MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                if (userChoice == DialogResult.Yes)
                {
                    HostFileManager.FileOperationResult createResult = HostFileManager.CreateDefaultHosts();
                    if (createResult == HostFileManager.FileOperationResult.UnauthorizedAccess)
                    {
                        MessageBox.Show(Resources.msg_error_unauthorized_user, Resources.error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else if (createResult == HostFileManager.FileOperationResult.Failed)
                    {
                        MessageBox.Show(Resources.msg_error_writting, Resources.error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }

                break;

            case HostFileManager.FileOperationResult.Failed:
                MessageBox.Show(Resources.msg_error_writting, Resources.error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                break;
            }
        }
        public byte[] GetChecksum(string fileName, Guid md5HashGuid)
        {
            byte[] hashData = null;

            if (HostFileManager != null)
            {
                object docData = HostFileManager.GetFileDocData(fileName);
                IPersistFileCheckSum fileChecksummer = docData as IPersistFileCheckSum;
                if (fileChecksummer != null)
                {
                    byte[] tempBytes = new byte[1024];
                    int    actualSize;
                    fileChecksummer.CalculateCheckSum(md5HashGuid, tempBytes.Length, tempBytes, out actualSize);
                    hashData = new byte[actualSize];
                    for (int i = 0; i < actualSize; i++)
                    {
                        hashData[i] = tempBytes[i];
                    }
                }
            }
            if (hashData == null)
            {
                HashAlgorithm MD5cryptoProvider = new MD5CryptoServiceProvider();
                using (Stream fileStream = File.OpenRead(fileName))
                {
                    hashData = MD5cryptoProvider.ComputeHash(fileStream);
                }
            }
            return(hashData);
        }
Exemplo n.º 5
0
        public byte[] GetChecksum(string fileName, Guid hashGuid)
        {
            byte[] hashData = null;

            if (HostFileManager != null)
            {
                object docData = HostFileManager.GetFileDocData(fileName);
                IPersistFileCheckSum fileChecksummer = docData as IPersistFileCheckSum;
                if (fileChecksummer != null)
                {
                    byte[] tempBytes = new byte[1024];
                    int    actualSize;
                    fileChecksummer.CalculateCheckSum(hashGuid, tempBytes.Length, tempBytes, out actualSize);
                    hashData = new byte[actualSize];
                    for (int i = 0; i < actualSize; i++)
                    {
                        hashData[i] = tempBytes[i];
                    }
                }
            }
            if (hashData == null)
            {
                HashAlgorithm hashAlgorithm;

                if (hashGuid == s_hashSHA256Guid)
                {
                    hashAlgorithm = SHA256.Create();
                }
                else if (hashGuid == s_hashSHA1Guid)
                {
                    hashAlgorithm = SHA1.Create();
                }
                else if (hashGuid == s_hashMD5Guid)
                {
                    hashAlgorithm = MD5.Create();
                }
                else
                {
                    hashAlgorithm = null;
                }

                if (hashAlgorithm != null)
                {
                    using (Stream fileStream = File.OpenRead(fileName))
                    {
                        hashData = hashAlgorithm.ComputeHash(fileStream);
                    }
                }
            }
            return(hashData);
        }
Exemplo n.º 6
0
        private void Save()
        {
            HostFileManager.FileOperationResult backupResult = HostFileManager.CreateBackup();

            bool backupFailed = false;

            switch (backupResult)
            {
            case HostFileManager.FileOperationResult.UnauthorizedAccess:
            case HostFileManager.FileOperationResult.BackupCreateFolderUnauthorizedAccess:
                backupFailed = true;
                break;

            case HostFileManager.FileOperationResult.Failed:
            case HostFileManager.FileOperationResult.BackupCreateFolderFailed:
                backupFailed = true;
                break;
            }

            // ------------------------------------------------------------
            if (backupFailed)
            {
                DialogResult userChoice = MessageBox.Show(Resources.msg_warning_backup_failed, Resources.warning, MessageBoxButtons.YesNo, MessageBoxIcon.Error);
                if (userChoice == DialogResult.No)
                {
                    return;
                }
            }

            // ------------------------------------------------------------

            HostFileManager.FileOperationResult writeResult = HostFileManager.WriteHosts(this.hosts);
            switch (writeResult)
            {
            case HostFileManager.FileOperationResult.UnauthorizedAccess:
                MessageBox.Show(Resources.msg_error_unauthorized_user, Resources.error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                break;

            case HostFileManager.FileOperationResult.Failed:
                MessageBox.Show(Resources.msg_error_writting, Resources.error, MessageBoxButtons.OK, MessageBoxIcon.Error);
                break;

            case HostFileManager.FileOperationResult.Success:
                this.modifed = false;
                CheckSaveStatus();
                break;
            }
        }
Exemplo n.º 7
0
        //
        // Deletes the specified file
        //
        public void Delete(string fileName)
        {
            if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }

            if (IsFileInHostManager(fileName))
            {
                HostFileManager.Delete(fileName);
            }
            else
            {
                File.Delete(fileName);
            }
        }
Exemplo n.º 8
0
        //
        // Checks to see if file exists
        //
        public bool Exists(string fileName)
        {
            bool fileExists = false;

            if (fileName == null)
            {
                throw new ArgumentNullException("fileName");
            }

            if (HostFileManager != null)
            {
                fileExists = HostFileManager.Exists(fileName, IsRealBuild);
            }
            else
            {
                fileExists = File.Exists(fileName);
            }

            return(fileExists);
        }
Exemplo n.º 9
0
        //
        // Get the last modificatin time for a given file.
        //
        public DateTime GetLastChangeTime(string srcFile)
        {
            DateTime lastChangeDT = new DateTime(0);

            if (String.IsNullOrEmpty(srcFile))
            {
                throw new ArgumentNullException("srcFile");
            }

            if (IsFileInHostManager(srcFile))
            {
                long fileTime = HostFileManager.GetFileLastChangeTime(srcFile);
                lastChangeDT = DateTime.FromFileTime(fileTime);
            }
            else
            {
                lastChangeDT = File.GetLastWriteTime(srcFile);
            }

            return(lastChangeDT);
        }