0
내 응용 프로그램의 출력을 보여주는 DataGridView가있는 WindowsForm이 있습니다. 버튼이있는 클래스는 DriveRecursion_Results.cs입니다. 그래서 버튼이 사용자에 의해 푸시되면 내 SanitizeFileNames 클래스의 FileCleanUp() 메서드가 호출되기를 원합니다. 나는 이것을 어떻게하는지 잘 모르겠습니다.다른 클래스의 버튼에서 내 클래스 및 메서드 호출
다음public partial class DriveRecursion_Results : Form
{
public DriveRecursion_Results()
{
InitializeComponent();
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
public void DriveRecursion(string retPath)
{
//recurse through files. Let user press 'ok' to move onto next step
// string[] files = Directory.GetFiles(retPath, "*.*", SearchOption.AllDirectories);
string pattern = " *[\\~#%&*{}/<>?|\"-]+ *";
//string replacement = "";
Regex regEx = new Regex(pattern);
string[] fileDrive = Directory.GetFiles(retPath, "*.*", SearchOption.AllDirectories);
List<string> filePath = new List<string>();
dataGridView1.Rows.Clear();
try
{
foreach (string fileNames in fileDrive)
{
if (regEx.IsMatch(fileNames))
{
string fileNameOnly = Path.GetFileName(fileNames);
string pathOnly = Path.GetDirectoryName(fileNames);
DataGridViewRow dgr = new DataGridViewRow();
filePath.Add(fileNames);
dgr.CreateCells(dataGridView1);
dgr.Cells[0].Value = pathOnly;
dgr.Cells[1].Value = fileNameOnly;
dataGridView1.Rows.Add(dgr);
filePath.Add(fileNames);
}
else
{
DataGridViewRow dgr2 = new DataGridViewRow();
dgr2.Cells[0].Value = "No Files To Clean Up";
dgr2.Cells[1].Value = "";
}
}
}
catch (Exception e)
{
StreamWriter sw = new StreamWriter(retPath + "ErrorLog.txt");
sw.Write(e);
}
}
private void button1_Click(object sender, EventArgs e)
{
//i want to call SanitizeFileName's method FileCleanup here.
}
}
가 SanitizeFileNames입니다 :
public class SanitizeFileNames
{
public void FileCleanup(List<string>filePath)
{
string regPattern = "*[\\~#%&*{}/<>?|\"-]+*";
string replacement = "";
Regex regExPattern = new Regex(regPattern);
foreach (string files2 in filePath)
{
try
{
string filenameOnly = Path.GetFileName(files2);
string pathOnly = Path.GetDirectoryName(files2);
string sanitizedFileName = regExPattern.Replace(filenameOnly, replacement);
string sanitized = Path.Combine(pathOnly, sanitizedFileName);
//write to streamwriter
System.IO.File.Move(files2, sanitized);
}
catch (Exception ex)
{
//write to streamwriter
}
}
}
}
SWeko는 올바른 구문을가집니다. SanitizeFileNames가 App_Code에 있거나 별도의 어셈블리에 있습니까? –
하지만 내 FileCleanup() 메서드는 파일 이름을 변경할 수 있도록 DriveRecursion 경로에서 filePath를 참조해야합니다. 다른 클래스의 filePath를 어떻게 참조 할 수 있습니까? – yeahumok
@yeahumok - 발신자 (버튼 클릭 이벤트)가 전달해야합니다. 어떻게 DriveRecursion이 호출 되었습니까? 클릭 이벤트 핸들러는 같은 클래스에 있기 때문에 경로 정보에도 액세스해야합니다. – GalacticCowboy