# Change directory

• (2 Pages)
• 1
• 2

## 20 Replies - 1156 Views - Last Post: 19 November 2019 - 01:32 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=417765&amp;s=543743f8a3173e6c3415ca319a1c2927&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 albert003

Reputation: 37
• Posts: 797
• Joined: 15-December 14

# Change directory

Posted 07 November 2019 - 12:51 PM

My new job is asking me to learn C# as well as work as a python programmer. I am in the middle of writing a program to automate the maintenance of the computers at work. I know I could make a command in the windows to do it, but I would like to try and figure out how to do it myself.

I know in C++ to change directories I could do something like this:
    std::cout << "Which directory do you want to enter?:" << std::endl;
std::string change;
std::cin >> change;
_chdir(change.c_str());
system("dir");


I tried troubleshooting it as well as looking up in google and I can't find a way to have the user select which directory they can enter. If someone would please give me a hint or a suggestion I would really appreciate it

Dreamincode wont let me show my code, every time I try it blocks me saying I am a hacker.

This post has been edited by albert003: 07 November 2019 - 12:53 PM

Is This A Good Question/Topic? 0

## Replies To: Change directory

### #2 modi123_1

• Suitor #2

Reputation: 15434
• Posts: 61,849
• Joined: 12-June 08

## Re: Change directory

Posted 07 November 2019 - 12:56 PM

Is this a winform, wpf, console app, or what?

### #3 albert003

Reputation: 37
• Posts: 797
• Joined: 15-December 14

## Re: Change directory

Posted 07 November 2019 - 01:00 PM

console app

Would you mind terribly if I tried to message you my program? Maybe it will let me do that without blocking me.

### #4 modi123_1

• Suitor #2

Reputation: 15434
• Posts: 61,849
• Joined: 12-June 08

## Re: Change directory

Posted 07 November 2019 - 01:03 PM

A directory is just a string path. There's nothing to select in a console app unless you spit out some numbered options and have the user pick from those.

### #5 modi123_1

• Suitor #2

Reputation: 15434
• Posts: 61,849
• Joined: 12-June 08

## Re: Change directory

Posted 07 November 2019 - 01:14 PM

Example of what I mean:

Current Directory: C:\test
Available:
0 : C:\test\abc
1 : C:\test\def
2 : C:\test\old_river
3 : C:\test\up
Select one to open:
2
Selected: 2 : C:\test\old_river
-----------------
Current Directory: C:\test\old_river
Available:
No directories

Go back up a directory?



That's all just strings, a few loops, and string arrays utilizing the 'Directory.GetDirectories' functionality.

### #6 Skydiver

• Code herder

Reputation: 7192
• Posts: 24,371
• Joined: 05-May 12

## Re: Change directory

Posted 07 November 2019 - 06:30 PM

This:
std::string change;
std::cin >> change;



Is simply:
string change = Console.ReadLine();



_chdir() is simply a matter of setting the Enviroment.CurrentDirectory.

### #7 albert003

Reputation: 37
• Posts: 797
• Joined: 15-December 14

## Re: Change directory

Posted 07 November 2019 - 10:20 PM

That was such a simple solution I feel like an idiot for not thinking about it earlier. Thank you everyone, I will tried to post my solution here but the website keeps blocking me thinking I am trying to hack the system.

What I am trying to do is have the program go to cmd, let the user select which directory and then it will run chkdisk and defragmentor.

skydiver
I review the pop machine program and I couldn't find the reference you mentioned. Would you please be so kind to tell me which post it was in?

### #8 Skydiver

• Code herder

Reputation: 7192
• Posts: 24,371
• Joined: 05-May 12

## Re: Change directory

Posted 07 November 2019 - 10:48 PM

Post #9. Lines 27 and 95.

### #9 albert003

Reputation: 37
• Posts: 797
• Joined: 15-December 14

## Re: Change directory

Posted 11 November 2019 - 03:39 PM

SkyDiver
I didn't realise I could use that in command using C#.

modi123_1
That actually makes my life a lot easier.

This is the problem I have with the program. I need to run cmd as an administrator so I can use chk dsk and disk defragmantor. I figured out a way to do that but I cannot change directories to the path the user wanted. I troubleshooted my program and went through a few tutorials on my own and I was able to change directories without running cmd as an administrator. I've tried everything I could thing of to show my work and I keep getting blocked. I put it on githiub this is the link to the program. I put only the function Maintenance.

https://github.com/a...03?tab=projects

### #10 modi123_1

• Suitor #2

Reputation: 15434
• Posts: 61,849
• Joined: 12-June 08

## Re: Change directory

Posted 11 November 2019 - 03:50 PM

The site is correct. You cannot upload an .EXE here.

https://github.com/a...03/copy-program

### #11 Skydiver

• Code herder

Reputation: 7192
• Posts: 24,371
• Joined: 05-May 12

## Re: Change directory

Posted 11 November 2019 - 08:52 PM

I feel that you are failing to tell us one your requirements or something that you are doing. Defrag and Chkdsk are volume level tools. It doesn't matter what directory you are in top use them.

Also, what is the folder you are trying to change to? How are you observing that the program failed to change directors when as admin, and succeeds when a normal user?

### #12 albert003

Reputation: 37
• Posts: 797
• Joined: 15-December 14

## Re: Change directory

Posted 12 November 2019 - 06:28 PM

modi123_1

I tried to copy and paste my code as well as putting my code on github. Both ideas I was blocked from posting my code here.

SkyDiver

My Idea is to make the C# program let the user choose which directory they wanted to enter and have it run chkdsk and disk defrag.

Since the site won't let me post my code my idea is something like this:

"Which directory do you want to enter?:"
string path;

changes to the directory the user wanted.
program runs chk dsk and disk defrag

I figured out how to run the program in cmd and change directory. But when I run it as an administrator, I can't figure out how to do it. I know I can make Windows do it, but I wanted to be proactive and have the user do it when they wanted.

### #13 Skydiver

• Code herder

Reputation: 7192
• Posts: 24,371
• Joined: 05-May 12

## Re: Change directory

Posted 12 November 2019 - 08:57 PM

Let's step back: Why do you need to change directories? It doesn't matter if I run chkdsk or defrag from "C:\" or from "C:\Users\Robert\Downloads". Both programs will still ask me which drive needs to be checked or defragmented.

For git, most people just upload source code, not binaries. If you are trying to upload binaries you are likely doing something wrong with source control.

string path;



Why are you trying to get a string from the user using ReadLine(), then converting it to an Int32, then try to assign it to the the variable path which is a string. Shouldn't you just assign the the string directly?

### #14 Skydiver

• Code herder

Reputation: 7192
• Posts: 24,371
• Joined: 05-May 12

## Re: Change directory

Posted 13 November 2019 - 08:29 AM

Anyway, I don't seem to have any problems changing directories while as a regular user or as an admin:

Spoiler

Are you sure that you don't have some kind of AntiVirus software, or nanny-ware that is blocking your application from doing certain things while running as administrator?

### #15 albert003

Reputation: 37
• Posts: 797
• Joined: 15-December 14

## Re: Change directory

Posted 13 November 2019 - 09:48 PM

Skydiver
I know I could do it in command and its not hard to do. I wanted to make it user friendly as an easy program to make for my new job and use C#.

I have Norton anti virus on my laptop and I don't have any nanny ware installed either. I'll try using my desktop to post my program and see if it blocks me again.

I have a few new ideas how to make the program work, I'll give them a try and if by Monday I cannot figure it out; I'll take a peek at your spoiler.