簡體   English   中英

System.NotSupportedException: '不支持指定的路徑格式。'

[英]System.NotSupportedException: 'The specified path format is not supported.'

我在表中有一個字段,需要填寫 XML 文件的路徑和結尾,以便在名為DONE的目錄中創建一個新文件。 這樣做是為了稍微整理目錄,因為完成的目錄不需要位於同一目錄中,因此它們可以從一個地方復制到另一個地方。

為什么會出現這個錯誤?

System.NotSupportedException: '不支持指定的路徑格式。'

Console.WriteLine("Ficheiro processado: " + filename);
string rootFolderPath = @"C:\XMLFiles";
string destinationPath = @"C:\XMLFiles\DONE";
string[] fileList = Directory.GetFiles(rootFolderPath);
foreach (string file1 in fileList)
{
    string fileToMove = rootFolderPath + file1;
    string moveTo = destinationPath + file1;
    File.Move(fileToMove, moveTo);
    da.SP_Insert(filename, file.Name, batch.BatchClassName, batch.Name, batch.Description, 0, "", 1, moveTo );
} 

function Directory.GetFiles(rootFolderPath); 返回文件的完整路徑,即文件名和目錄。 如果像您嘗試的那樣,只需要文件名,則需要提取它。

FileInfo class 非常擅長僅提取完整路徑的文件名。

foreach (string file1 in fileList)
{
    FileInfo fi = new FileInfo(file1);
    string moveTo = Path.Combine( destinationPath, fi.Name);
    File.Move(file1, moveTo);
}

不要使用string fileToMove = rootFolderPath + file1 ,而是嘗試使用System.IO.Path.Combine

var fileToMove  = Path.Combine(rootFolderPath, file1);
var moveTo = Path.Combine(destinationPath , file1);

GetFiles返回完整路徑 不僅僅是文件名:

返回指定目錄中的文件名(包括它們的路徑

因此,對於源文件,您不需要組合任何內容,而對於目標路徑,您需要在組合之前先拆分文件名:

foreach (string file1 in fileList)
{
    string moveTo = Path.Combine(destinationPath, Path.GetFileName(file1));
    File.Move(file1, moveTo);
    // ...
} 

這個問題是這樣解決的。

                    using (OpenFileDialog openFileDialog1 = new OpenFileDialog())
                    {

                        if (openFileDialog1.ShowDialog() == DialogResult.OK)
                        {
                            string CaminhoInicial = openFileDialog1.FileName;
                            Guid g = Guid.NewGuid();
                            FileInfo fi = new FileInfo(CaminhoInicial);
                            File.Copy(CaminhoInicial, CaminhoFinal + g.ToString() + fi.Extension, true);
                            da.SP_Inserir_Imagem(id, g.ToString() + fi.Extension);
                        }

                    }

                    try
                    {


                        if (dt.Rows.Count != 0)
                        {
                            string NomeImagem = dt.Rows[0][0].ToString();
                            pictureBox1.Image = Image.FromFile(CaminhoFinal + NomeImagem.Replace(" ", ""));
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message + "    ||||    " + ex.StackTrace, "Erro", MessageBoxButtons.OK);
                    }

另請閱讀此 Microfost Docs 帖子以獲取更多上下文。 https://docs.microsoft.com/en-us/dotnet/api/system.io.fileinfo?view=net-5.0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM