Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3323972
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:21:58+00:00 2026-05-17T23:21:58+00:00

As part of a huge refactoring I removed some duplicate classes and enums. I

  • 0

As part of a huge refactoring I removed some duplicate classes and enums. I moved namespaces and restructured everything to be easier to maintain in the future.

All changes has been scripted already except for one thing. I need to insert a data contract namespace in every file that uses another namespace if the data contract namespace has not been inserted yet.

The code I have at the moment does not work but is sort of what I need I guess.

function Insert-Usings{
    trap {
        Write-Host ("ERROR: " + $_) -ForegroundColor Red
        return $false 
    }
    (Get-ChildItem $base_dir -Include *.asmx,*.ascx,*.cs,*.aspx -Force -Recurse -ErrorAction:SilentlyContinue) | % {
    $fileName  = $_.FullName
    (Get-Content $fileName) | 
        Foreach-Object 
        {
            $_
            if ($_ -cmatch "using Company.Shared;") { 
                    $_ -creplace "using Company.Shared;", "using Company.Common;"
            }
            elseif ($_ -cmatch "using Company") {
                #Add Lines after the selected pattern 
                "using Company.Services.Contracts;"
            }
            else{
                $_
            }
        }
    } | Set-Content $fileName
}

Edit: The code tends to output (overwrite the whole file with-) “using Company.Services.Contracts” statements.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:21:58+00:00Added an answer on May 17, 2026 at 11:21 pm

    It is not quite clear what you what to get exactly but I’ll try to guess, see my comments in the code. The original code, I think, contains a few mistakes, one is serious: Set-Content is used in a wrong pipeline/loop. Here is the corrected code.

    function Insert-Usings
    {
        trap {
            Write-Host ("ERROR: " + $_) -ForegroundColor Red
            return $false
        }
        (Get-ChildItem $base_dir -Include *.asmx,*.ascx,*.cs,*.aspx -Force -Recurse -ErrorAction:SilentlyContinue) | % {
            $fileName  = $_.FullName
            (Get-Content $fileName) | % {
                if ($_ -cmatch "using Company\.Shared;") {
                    # just replace
                    $_ -creplace "using Company\.Shared;", "using Company.Common;"
                }
                elseif ($_ -cmatch "using Company") {
                    # write the original line
                    $_
                    # and add this after
                    "using Company.Services.Contracts;"
                }
                else{
                    # write the original line
                    $_
                }
            } |
            Set-Content $fileName
        }
    }
    

    For example, it replaces this:

    xxx
    
    using Company.Shared;
    
    using Company;
    
    ttt
    

    with this:

    xxx
    
    using Company.Common;
    
    using Company;
    using Company.Services.Contracts;
    
    ttt
    

    Note: presumably you should not apply this code to sources more than once, the code is not designed for this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm refactoring some piece of... code, part of huge, complex .Net project. There is
which part of huge package nltk I must study and use, if I need
I want to replace part of the following html text (excerpt of a huge
I have a huge grammar developed for pyparsing as part of a large, pure
I have a bitmap object which is taking a huge part of the memory
I have a set of flags which are part of a huge text data
Updated I have some huge data, which becomes a large table say table parent
I'm facing a huge challenge with some excel sheets which was written in 90's
I have a huge text file, each line seems like this: Some sort of
I develop part of some JBoss+EJB based enterprise application. My module needs to process

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.