2013-05-31 2 views
1

아래 앱이 있습니다. 나는 독자를 위해 더 쉬운 테스트를 위해 그것을 약간 수정했다. 파일 이름을 확장자로 설정하면 예를 들어 test.txt, 대화 상자에 의해 txt 확장명이 제거됩니다. 그러나 사용자가 확장 프로그램을 지정할 수있게하고 더 중요한 것은 확장 프로그램을 설정할 수 있기를 원합니다. 내가 해킹하는 한 가지 방법은 내가 가지고있는 확장을 기반으로 필터를 수정하는 것입니다. 이것이 유일한 방법입니까?SaveFileDialog는 파일 이름에서 확장자를 제거합니다.

VS 2010 Express를 사용하고 있습니다.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Globalization; 
using System.IO; 
using System.Windows; 
namespace SpeedDating 
{ 
    partial class Program 
    { 
     [STAThread] 
     static void Main(string[] args) 
     { 
      Form form = new Form(); 
      form.WindowState = FormWindowState.Minimized; 
      form.ShowInTaskbar = false; 
      form.TopMost = true; 
      form.Show(); 

      string filename = "test.txt"; 
      string ext = filename.Substring(filename.LastIndexOf('.')); 
      SaveFileDialog dialog = new SaveFileDialog(); 
      dialog.Title = "SpeedDating App"; 
      dialog.RestoreDirectory = true; 
      dialog.CheckFileExists = false; 
      dialog.CheckPathExists = false; 
      dialog.SupportMultiDottedExtensions = true; 
      dialog.AddExtension = false; 
      dialog.Filter = "All files (*.*)|*.*"; 
      dialog.FileName = DateTime.Now.ToString("yyyyMMdd") + ext; 

      DialogResult result = dialog.ShowDialog(); 
      if (result == DialogResult.OK && dialog.FileName != "") 
      { 
       try 
       { 
        FileStream outfs = File.Create(dialog.FileName); 
        FileStream infs = File.Open(filename, FileMode.Open); 
        infs.CopyTo(outfs); 
        infs.Close(); 
        outfs.Close(); 

        MessageBox.Show(form, "Copied file."); 
       } 
       catch (NotSupportedException ex) 
       { 
        MessageBox.Show(form, "Probably removed the original file."); 
       } 
      } 
      else if (result != DialogResult.Cancel) 
      { 
       MessageBox.Show(form, "No path found to write to."); 
      } 

      form.Close(); 
     } 
    } 
} 
+0

나는 당신의 코드를 복사했고, 메시지 박스에서 "폼"에 대한 참조 만 변경했다. 그것은 나를 위해 잘 작동합니다. 나는 항상 대화 상자에 내가 입력 한 정확한 파일을 얻는다. – DonBoitnott

+0

아무 문제가 없지만 "형식"개체의 목적은 무엇일까요? 그것은 비어 있으며 아무것도하지 않습니다. 알고있는 MessageBox를 앵커링하지 않아도됩니다. – DonBoitnott

+0

내 시스템에서 사용자가 운영 체제 수준에서 설정 한 내용에 따라 대화 상자가 나타납니다. 확장 기능이 파일 탐색기에 숨겨져 있다면 대화 상자에 확장 기능이 숨겨지고 그 반대도 마찬가지입니다. –

답변

3

더 중요한 I 당신은 .DefaultExt(), .AddExtension(), .Filter을 설정할 수 있습니다

확장을 설정할 수 있도록하려면().FilterIndex() 속성 :

 string filename = "test.xyz"; 

     SaveFileDialog dialog = new SaveFileDialog(); 
     dialog.Title = "SpeedDating App"; 
     dialog.RestoreDirectory = true; 
     dialog.CheckFileExists = false; 
     dialog.CheckPathExists = false; 
     dialog.SupportMultiDottedExtensions = true; 
     dialog.Filter = "All files (*.*)|*.*"; 

     dialog.DefaultExt = System.IO.Path.GetExtension(filename); 
     if (dialog.DefaultExt.Length > 0) 
     { 
      dialog.AddExtension = true; 
      dialog.Filter = dialog.DefaultExt + " files (*." + dialog.DefaultExt + ")|*." + dialog.DefaultExt + "|" + dialog.Filter; 
      dialog.FilterIndex = 0; 
     } 

     dialog.FileName = DateTime.Now.ToString("yyyyMMdd"); 

     DialogResult result = dialog.ShowDialog(); 
     if (result == DialogResult.OK && dialog.FileName != "") 
     { 
      Console.WriteLine(dialog.FileName); 
     } 

* 파일 탐색기에서 "파일 확장명"을 표시하는 옵션이 해제 된 경우 대화 상자에서도 확장명을 숨길 수 있습니다 ... 그러나 위의 설정은 .FileName() 값에 확장명을 추가합니다 대화 상자에서 반환됩니다.

+0

대단히 고마워요! – RobotRock

관련 문제