コード例 #1
0
ファイル: CodeSerializer.cs プロジェクト: Potapy4/dotnet-wcf
        private StreamWriter CreateOutputFile()
        {
            OutputPathHelper.CreateDirectoryIfNeeded(_outputFilePath);

            try
            {
                return(new StreamWriter(new FileStream(_outputFilePath, FileMode.Create, FileAccess.Write), s_ouputEncoding));
            }
            catch (Exception e)
            {
                if (Utils.IsFatalOrUnexpected(e))
                {
                    throw;
                }
                throw new ToolRuntimeException(string.Format(SR.ErrCannotCreateFileFormat, _outputFilePath), e);
            }
        }
コード例 #2
0
ファイル: CodeSerializer.cs プロジェクト: Potapy4/dotnet-wcf
        public string SaveCode(CodeCompileUnit codeCompileUnit)
        {
            string filePath = null;

            OutputPathHelper.CreateDirectoryIfNeeded(_outputFilePath);

            CodeGeneratorOptions codeGenOptions = new CodeGeneratorOptions();

            codeGenOptions.BracingStyle = "C";

            using (TextWriter writer = CreateOutputFile())
            {
                try
                {
                    _codeProvider.GenerateCodeFromCompileUnit(codeCompileUnit, writer, codeGenOptions);
                    writer.Flush();
                }
                catch (Exception e)
                {
                    if (Utils.IsFatalOrUnexpected(e))
                    {
                        throw;
                    }

                    try
                    {
                        if (File.Exists(_outputFilePath))
                        {
                            File.Delete(_outputFilePath);
                        }
                    }
                    catch (System.UnauthorizedAccessException)
                    {
                    }

                    throw new ToolRuntimeException(SR.ErrCodegenError, e);
                }
                filePath = _outputFilePath.Contains(" ") ? string.Format(CultureInfo.InvariantCulture, "\"{0}\"", _outputFilePath) : _outputFilePath;
            }

            return(filePath);
        }