2

So I am attempting to change the creation date of a text file in C#. The user will type in thr creation date of the file, it will then change the text files creation date to what the user inputted. Trouble is it keeps adding a ' for some reason resulting in an error message, it is calling on Powershell to accomplish this:

    public partial class Form6 : Form
    {
        public Form6()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string dir = textBox1.Text;

            PowerShell ps = PowerShell.Create();



          ps.AddCommand("Get-ChildItem c:\\encryptedmessagehere.txt | % {$_.CreationTime = '"+ dir + "'}");

            ps.Invoke();
        }
    }
}

Trouble is, after the "'}");, it automatically adds another ', incorrecting the powershell command to change the date. Is there a way to stop it from adding the ' at the end?

The returned error is:

System.Management.Automation.CommandNotFoundException HResult=0x80131501 Message=The term 'Get - ChildItem C:\encryptedmessagehere.txt | % {$_.CreationTime = '06/12/12 09:27:03 AM'} ' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Thank you.

10
  • 5
    Get - ChildItem <- this is your (first, if not only) problem, it should be Get-ChildItem without any spaces. Commented Sep 22, 2020 at 7:08
  • "Is there a way to stop it from adding the ' at the end?" As far as I can see, that's just a delimiter inside the error message. Not a part of the actual command string. Commented Sep 22, 2020 at 7:25
  • Thanks I just tried that. The problem persists its adding the ' at the end, thanks. Commented Sep 22, 2020 at 7:25
  • 2
    Is it really necessary to use PowerShell for this? Is something stopping you using a FileInfo object and using that to update the date? Commented Sep 22, 2020 at 7:45
  • 4
    If datetime formatting is the all you're using PowerShell for in this solution then you are making things way too complicated. Commented Sep 22, 2020 at 8:32

2 Answers 2

10

In case anyone wants to know, you need to install the following NuGet packages to use PowerShell in C#:

  • Microsoft.PowerShell.SDK
  • System.Management.Automation

That's how I was able to get Jack J Jun's answer to work.

Sign up to request clarification or add additional context in comments.

Comments

8

First, you need to use Powershell.AddScript method instead of Powershell.AddCommand method.

Second, you can try the following Powershell code to change the creation time of the file.

(Get-ChildItem d:\Test\new.txt).CreationTime =  '2020/09/23'

Finally, you can try the following c# code example to call powershell code in c#.

private void button1_Click(object sender, EventArgs e)
        {
            string datetime= textBox1.Text;
            PowerShell ps = PowerShell.Create();
            string script = string.Format("(Get-ChildItem d:\\New.txt).CreationTime = '{0}'", datetime);
            ps.AddScript(script);
            ps.Invoke();
            MessageBox.Show("Test");
        }

1 Comment

@KeiferBly, I am glad to hear that your problem has been solved, you can click '✔' to mark my reply as an answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.