C# rename path name (1 Viewer)

Joined
Feb 19, 2010
Messages
157
Reaction score
174
Offline
Hey everyone:

I'm trying to rename files to begin with "1" as they are chosen but my 1 has been off. Could someone look at my code and let me know if they notice anything out of place or that's just flat wrong.

Thanks

Code:
  private void opnBtn_Click(object sender, EventArgs e)
        {
            string oldPath;

            OpenFileDialog fdlg = new OpenFileDialog();

            fdlg.InitialDirectory = @"H:\";
            fdlg.Filter = "All files (*.*), |*.*";
            fdlg.FilterIndex = 2;
            fdlg.RestoreDirectory = true;

            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                oldPath = fdlg.FileName;
                string backSlashSch = "\\";
                string backSlashPos = oldPath.Substring(0,    oldPath.LastIndexOf("\\") + 1);

                int backPos = oldPath.LastIndexOf(backSlashSch);

                int endOfPath = oldPath.Length;
                int calcPos = (endOfPath - backPos - 1);
                string newPath = oldPath.Insert(calcPos, "1");


                MessageBox.Show(oldPath);
                MessageBox.Show(backSlashPos);
                MessageBox.Show(backPos.ToString());
                MessageBox.Show(endOfPath.ToString());
                MessageBox.Show(calcPos.ToString());
                MessageBox.Show(newPath);

                pathDsp.Text= (oldPath + "      " + backSlashPos + "     " + backSlashPos + "     " + endOfPath + "     " + calcPos + "     " + newPath);
                
            }

            

            
        }
 
New problem. I need it to save automatically.
 
looks way more complicated than needs to be.
string .. = 1 + string will add a 1 to the start.
 
Its a user selected path that needs to be re-saved with "1" before each file that is used. Some of those lines are were to test and are not used.
The issue I have now is to save the file automatically when a "no" button is clicked for a separate delete box .
 
Did you figure this out? The System.IO namespace is your friend here. Use the File object to check to make sure the new path doesn't already exist (File.Exists). Then to do a rename, I think you actually have to perform a copy to the new location and a delete of the old location - I can't remember if there is a Move method that combines these operations for you.

Also, instead of this manual manipulation of the path names, use the Path object to get the directory path, filename, extension, etc. Use Path.Combine to put a directory path with a file name (this will automatically take care of the slashes for you) and Path.ChangeExtension (or whatever) to add or change the extension. Use the result of the Path's static methods as the filename to use with the File object.
 

Create an account or login to comment

You must be a member in order to leave a comment

Create account

Create an account on our community. It's easy!

Log in

Already have an account? Log in here.

Users who are viewing this thread

    Back
    Top Bottom